java做压缩或解压

用过java做压缩或解压的都知道,jdk提供的zip只能按UTF-8格式处理,所有jdk提供的zip不能支持中文文件名采用Apache的zip包解决中文文件名问题(依赖 ant-1.9.6.jar)



Java代码 复制代码 收藏代码
1.package cn.tzz.zip; 
2. 
3.import java.io.BufferedInputStream; 
4.import java.io.BufferedOutputStream; 
5.import java.io.File; 
6.import java.io.FileInputStream; 
7.import java.io.FileOutputStream; 
8.import java.io.IOException; 
9.import java.io.InputStream; 
10.import java.io.OutputStream; 
11.import java.util.Enumeration; 
12. 
13.import org.apache.tools.zip.ZipEntry; 
14.import org.apache.tools.zip.ZipFile; 
15.import org.apache.tools.zip.ZipOutputStream; 
16. 
17./**
18. * ZIP工具包(支持中文)
19. * 依赖:ant-1.9.6.jar
20. */ 
21.public class CHZipUtils { 
22. 
23.    /**使用GBK编码可以避免压缩中文文件名乱码*/ 
24.    private static final String CHINESE_CHARSET = "GBK"; 
25.    /**文件读取缓冲区大小*/ 
26.    private static final int CACHE_SIZE = 1024; 
27. 
28.    /**
29.     * 压缩文件
30.     * @param sourceFolder 压缩文件夹
31.     * @param zipFilePath 压缩文件输出路径
32.     */ 
33.    public static void zip(String sourceFolder, String zipFilePath) { 
34.        OutputStream os = null; 
35.        BufferedOutputStream bos = null; 
36.        ZipOutputStream zos = null; 
37.        try { 
38.            os = new FileOutputStream(zipFilePath); 
39.            bos = new BufferedOutputStream(os); 
40.            zos = new ZipOutputStream(bos); 
41.            // 解决中文文件名乱码 
42.            zos.setEncoding(CHINESE_CHARSET); 
43.            File file = new File(sourceFolder); 
44.            String basePath = null; 
45.            if (file.isDirectory()) {//压缩文件夹 
46.                basePath = file.getPath(); 
47.            } else { 
48.                basePath = file.getParent(); 
49.            } 
50.            zipFile(file, basePath, zos); 
51.             
52.        } catch (Exception e) { 
53.            e.printStackTrace(); 
54.        } finally{ 
55.            try { 
56.                if (zos != null) { 
57.                    zos.closeEntry(); 
58.                    zos.close(); 
59.                } 
60.                if (bos != null) { 
61.                    bos.close(); 
62.                } 
63.                if (os != null) { 
64.                    os.close(); 
65.                } 
66.            } catch (IOException e) { 
67.                e.printStackTrace(); 
68.            } 
69.        } 
70.    } 
71. 
72.    /**
73.     * 递归压缩文件
74.     * @param parentFile
75.     * @param basePath
76.     * @param zos
77.     * @throws Exception
78.     */ 
79.    private static void zipFile(File parentFile, String basePath, ZipOutputStream zos) throws Exception { 
80.        File[] files = new File[0]; 
81.        if (parentFile.isDirectory()) { 
82.            files = parentFile.listFiles(); 
83.        } else { 
84.            files = new File[1]; 
85.            files[0] = parentFile; 
86.        } 
87.        String pathName; 
88.        InputStream is; 
89.        BufferedInputStream bis; 
90.        byte[] cache = new byte[CACHE_SIZE]; 
91.        for (File file : files) { 
92.            if (file.isDirectory()) { 
93.                pathName = file.getPath().substring(basePath.length() + 1) + File.separator; 
94.                zos.putNextEntry(new ZipEntry(pathName)); 
95.                zipFile(file, basePath, zos); 
96.            } else { 
97.                pathName = file.getPath().substring(basePath.length() + 1); 
98.                is = new FileInputStream(file); 
99.                bis = new BufferedInputStream(is); 
100.                zos.putNextEntry(new ZipEntry(pathName)); 
101.                int nRead = 0; 
102.                while ((nRead = bis.read(cache, 0, CACHE_SIZE)) != -1) { 
103.                    zos.write(cache, 0, nRead); 
104.                } 
105.                bis.close(); 
106.                is.close(); 
107.            } 
108.        } 
109.    } 
110. 
111.    /**
112.     * 解压压缩包
113.     * @param zipFilePath 压缩文件路径
114.     * @param destDir 解压目录
115.     */ 
116.    public static void unZip(String zipFilePath, String destDir) { 
117.        ZipFile zipFile = null; 
118.        try { 
119.            BufferedInputStream bis = null; 
120.            FileOutputStream fos = null; 
121.            BufferedOutputStream bos = null; 
122.            zipFile = new ZipFile(zipFilePath, CHINESE_CHARSET); 
123.            Enumeration<ZipEntry> zipEntries = zipFile.getEntries(); 
124.            File file, parentFile; 
125.            ZipEntry entry; 
126.            byte[] cache = new byte[CACHE_SIZE]; 
127.            while (zipEntries.hasMoreElements()) { 
128.                entry = (ZipEntry) zipEntries.nextElement(); 
129.                if (entry.isDirectory()) { 
130.                    new File(destDir + entry.getName()).mkdirs(); 
131.                    continue; 
132.                } 
133.                bis = new BufferedInputStream(zipFile.getInputStream(entry)); 
134.                file = new File(destDir + entry.getName()); 
135.                parentFile = file.getParentFile(); 
136.                if (parentFile != null && (!parentFile.exists())) { 
137.                    parentFile.mkdirs(); 
138.                } 
139.                fos = new FileOutputStream(file); 
140.                bos = new BufferedOutputStream(fos, CACHE_SIZE); 
141.                int readIndex = 0; 
142.                while ((readIndex = bis.read(cache, 0, CACHE_SIZE)) != -1) { 
143.                    fos.write(cache, 0, readIndex); 
144.                } 
145.                bos.flush(); 
146.                bos.close(); 
147.                fos.close(); 
148.                bis.close(); 
149.            } 
150.        } catch (IOException e) { 
151.            e.printStackTrace(); 
152.        }finally{ 
153.            try { 
154.                zipFile.close(); 
155.            } catch (IOException e) { 
156.                e.printStackTrace(); 
157.            } 
158.        } 
159.    } 
160. 
161.    public static void main(String[] args) throws Exception { 
162.//      String sourceFolder = "D:/test/1.txt"; 
163.//      String sourceFolder = "D:/test/中文名.txt"; 
164.        String sourceFolder = "D:/test/cms"; 
165.        String zipFilePath = "D:/test/zip/压缩文件.zip"; 
166.        String destDir = "D:/test/zip/"; 
167.        CHZipUtils.zip(sourceFolder, zipFilePath); 
168.//      CHZipUtils.unZip(zipFilePath, destDir); 
169.        System.out.println("********执行成功**********"); 
170.    } 
171. 
172.} 

猜你喜欢

转载自weitao1026.iteye.com/blog/2266115
今日推荐