yangys
2025-08-06 1911be8941e5fe2705c2c56e74e52bd426468793
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
 
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.springblade.core.oss.OssTemplate;
import org.springblade.core.tenant.annotation.NonDS;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.FileUtil;
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;
    @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);
 
        /*
        try (ZipFile zipFile = new ZipFile(tempFile.toFile())) {
            ZipEntry entry = zipFile.getEntry(entryn);
            InputStream ins = zipFile.getInputStream(entry);
 
            Path outputPath = Paths.get("d:/downtest.txt");
            Files.copy(ins, outputPath, StandardCopyOption.REPLACE_EXISTING);
        }
 
            List<String> entryNameList = new ArrayList<>();
        try (SeekableInMemoryByteChannel channel = new SeekableInMemoryByteChannel(bytes);
             ZipFile zipFile = new ZipFile(channel)) {
 
            ZipArchiveEntry entry;
            Enumeration<ZipArchiveEntry> entries = zipFile.getEntries();
            String entryn = "CP1-13-1/CP1-13-1-1-1-1.txt";
            while (entries.hasMoreElements()) {
                entry = entries.nextElement();
                entryNameList.add(entry.getName());
            }
        }*/
            /*
        try (SeekableInMemoryByteChannel channel = new SeekableInMemoryByteChannel(bytes);
             ZipFile zipFile = new ZipFile(channel)) {
            List<String> dirList = entryNameList.stream().filter(s -> s.endsWith("/")).toList();
            for(String dir : dirList) {
                entryNameList.stream().filter(s -> s.startsWith(dir)).forEach(entryName -> {
                    ZipArchiveEntry fileEntry = zipFile.getEntry(entryName);
                    //ZipArchiveEntry fileEntry = entryMap.get(entryName);
                    String fileName = StringUtils.removeStart(entryName,dir);//去除文件名路径部分
                    try {
                        InputStream ins = zipFile.getInputStream(fileEntry);
                        ByteArrayInputStream byteS = new ByteArrayInputStream(ins.readAllBytes());
 
                        Path outputPath = Paths.get("d:/downtest.txt");
                        Files.copy(byteS, outputPath, StandardCopyOption.REPLACE_EXISTING);
                        byteS.reset();
 
                        BladeFile newOssFile = ossTemplate.putFile("mdm", fileName, byteS);
                        r.setData(newOssFile.getName());
                        System.out.println(newOssFile.getName());
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                });
            }
        }*/
        return r;
    }
 
}