yangys
2025-09-20 fcee672452c02cc29e0e17ebc27a8c51698c6d0d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
 
package org.springblade.mdm.test;
 
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springblade.core.oss.OssTemplate;
import org.springblade.core.oss.model.BladeFile;
import org.springblade.core.tenant.annotation.NonDS;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.FileUtil;
import org.springblade.mdm.program.service.ProgramAnnotationService;
import org.springblade.mdm.utils.FileContentUtil;
import org.springblade.system.pojo.entity.Dict;
import org.springblade.system.pojo.entity.DictBiz;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
import java.io.*;
import java.net.URI;
import java.nio.file.*;
import java.util.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
 
/**
 * 流程管理接口
 *
 * @author Chill
 */
@NonDS
@RestController
@RequestMapping("/test/")
@AllArgsConstructor
@Tag(name = "MDM系统接口", description = "引入blade-system的接口")
@Slf4j
public class OssTestController {
    private final OssTemplate ossTemplate;
    private final ProgramAnnotationService programAnnotationService;
    @GetMapping("/readzip")
    @ApiOperationSupport(order = 2)
    @Operation(summary = "读zip")
    public R<String> readzip(@Parameter(description = "机构父节点id") Long deptId) throws IOException {
        R<String> r = R.data("");
        InputStream inputStream = new FileInputStream("d:/spd.zip");
        byte[] zipData = FileUtil.copyToByteArray(inputStream);
        Path tempFile = Files.createTempFile("tempzip", ".zip");
        // 写入字节数据到临时文件
        Files.write(tempFile, zipData, StandardOpenOption.WRITE);
 
        Map<String, String> env = new HashMap<>();
        env.put("create", "true");
        URI uri = URI.create("jar:file:/inmemory.zip");
        List<String> entryNameList = new ArrayList<>();
        String entryn = "CP1-13-1/CP1-13-1-1-1-1.txt";
        try (ZipFile zipFile = new ZipFile(tempFile.toFile())) {
            Enumeration<? extends ZipEntry> entris = zipFile.entries();
            ZipEntry entry;
            while(entris.hasMoreElements()) {
                entry = entris.nextElement();
                entryNameList.add(entry.getName());
            }
            //便利完成直接取树
            entry = zipFile.getEntry(entryn);
            InputStream ins = zipFile.getInputStream(entry);
            Path outputPath = Paths.get("d:/downtest.txt");
            Files.copy(ins, outputPath, StandardCopyOption.REPLACE_EXISTING);
        }
 
        System.out.println(entryNameList);
 
 
        return r;
    }
 
    @GetMapping("/replace")
    @ApiOperationSupport(order = 2)
    @Operation(summary = "替换内容")
    public R<String> replace(String filepath) throws IOException {
        File file = new File(filepath);
        FileInputStream fis = new FileInputStream(file);
        InputStream newins = FileContentUtil.replaceAtLine(fis,2,"GHTEST");
 
        BladeFile bfile = ossTemplate.putFile("replaceok.txt",newins);
        return R.data(bfile.getLink()+","+bfile.getName());
    }
    /*
    @GetMapping("/replace2")
    @ApiOperationSupport(order = 2)
    @Operation(summary = "替换内容2")
    public R<String> replace() throws IOException {
        String ossName = "upload/20250816/fb971fa2186b5572443687e9fa425123.txt";//这是里面已有的文件
 
        String filename = "CP3-1-1-1.txt";
        String machineGroup = "FANUC";
        List<DictBiz> annoDicts = programAnnotationService.getAnnotionList();
        BladeFile bfile = null;
        try(InputStream ins = ossTemplate.statFileStream(ossName);){
            byte[] bytes = IOUtils.toByteArray(ins);
            ByteArrayInputStream byteins =  new ByteArrayInputStream(bytes);
            String annoTxt = programAnnotationService.generateAnnotation("GH",machineGroup,annoDicts);
            int statusLineIndex = 2;
            String line2 = FileContentUtil.readLineAt(byteins,statusLineIndex);//第三行应该是状态注释
            byteins.reset();
            InputStream finishedStream;
            if(programAnnotationService.isAnnotation(line2,machineGroup,annoDicts)){
                finishedStream = FileContentUtil.replaceAtLine(byteins,statusLineIndex,annoTxt);
            }else{
                finishedStream = FileContentUtil.insertLine(byteins,statusLineIndex,annoTxt);
            }
 
            try(finishedStream) {
                finishedStream.reset();
                bfile = ossTemplate.putFile(filename, finishedStream);
                //替换原有的文件地址
                //flowProgramFile.setOssName(bfile.getName());
            }
        }
 
        InputStream ins  = ossTemplate.statFileStream(ossName);
        InputStream newins = FileContentUtil.replaceAtLine(fis,2,"GHTEST");
 
        BladeFile bfile = ossTemplate.putFile("replaceok.txt",newins);
 
 
        return R.data(bfile.getLink()+","+bfile.getName());
    }
*/
}