yuxc
2025-01-15 c09f81131e8b7c83937206d7cf76f34d2020be75
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
package com.vci.client.workflow.editor;
 
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.jar.JarEntry;
import java.util.jar.JarOutputStream;
 
public class ZipUtil {
 
    // 调用该方法生成zip文件
    public static void saveZip(String fileName, Map<String, InputStream> dataMap) {
        try {
            FileOutputStream fos = new FileOutputStream(fileName);
            JarOutputStream jos = new JarOutputStream(fos);
            byte buf[] = new byte[256];
            if (dataMap != null) {
                Set<Entry<String, InputStream>> entrySet = dataMap.entrySet();
                int len = -1;
                for (Entry<String, InputStream> entry : entrySet) {
                    String name = entry.getKey();
                    InputStream inputStream = entry.getValue();
                    if (name != null && inputStream != null) {
                        BufferedInputStream bis = new BufferedInputStream(
                                inputStream);
                        JarEntry jarEntry = new JarEntry(name);
                        jos.putNextEntry(jarEntry);
 
                        while ((len = bis.read(buf)) >= 0) {
                            jos.write(buf, 0, len);
                        }
 
                        bis.close();
                        jos.closeEntry();
                    }
                }
            }
            jos.flush();
            jos.close();
            fos.flush();
            fos.close();
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }
 
    public static byte[] transformInputstream(FileInputStream input) throws Exception {
        byte[] byt = null;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        int b = 0;
        b = input.read();
        while (b != -1) {
            baos.write(b);
            b = input.read();
        }
        byt = baos.toByteArray();
        return byt;
    }
 
}