日期:2014-05-16  浏览次数:20935 次

java打包linux文件 .
01.   前几天写了文章介绍java文件夹的压缩(见http://zwllxs.javaeye.com/blog/836483),但是发现一个问题,在linux下,如果文件夹内的文件是中文名,那么压缩后,这些文件名全部成为了乱码而无法解压,要能支持linux压缩,有两点:    
02.1:再压缩前,要设置linux模式,   
03.  如果是文件目录,则   
04.Java代码   
05.ZipEntry zipEntry=new ZipEntry(basePath + System.getProperties().getProperty("file.separator"));     
06.zipEntry.setUnixMode(755);//解决linux乱码     
07.  
08.ZipEntry zipEntry=new ZipEntry(basePath + System.getProperties().getProperty("file.separator"));  
09.zipEntry.setUnixMode(755);//解决linux乱码   
10.如果是文件,则   
11.Java代码   
12.ZipEntry zipEntry=new ZipEntry(base);     
13. zipEntry.setUnixMode(644);//解决linux乱码     
14.  
15.ZipEntry zipEntry=new ZipEntry(base);  
16. zipEntry.setUnixMode(644);//解决linux乱码   
17.  
18.然后在输出时强制设置编码:   
19.Java代码   
20.ZipOutputStream out = new ZipOutputStream(new FileOutputStream(     
21.               zipFileName));     
22.       out.setEncoding("GBK");//解决linux乱码     
23.  
24. ZipOutputStream out = new ZipOutputStream(new FileOutputStream(  
25.                zipFileName));  
26.        out.setEncoding("GBK");//解决linux乱码   
27.  
28.全部代码如下:   
29.  
30.Java代码   
31.import java.io.File;     
32.import java.io.FileInputStream;     
33.import java.io.FileOutputStream;     
34.import java.io.OutputStream;     
35.    
36.import org.apache.tools.zip.ZipEntry;     
37.import org.apache.tools.zip.ZipOutputStream;     
38.    
39./**   
40. * 文件夹压缩,支持win和linux   
41. * @author wlzhang   
42. *   
43. */    
44.public class ZipUtil     
45.{     
46.    /**   
47.     * @param inputFileName   
48.     *            输入一个文件夹   
49.     * @param zipFileName   
50.     *            输出一个压缩文件夹,打包后文件名字   
51.     * @throws Exception   
52.     */    
53.    public static OutputStream zip(String inputFileName, String zipFileName) throws Exception     
54.    {     
55.        return zip(zipFileName, new File(inputFileName));     
56.    }     
57.    
58.    private static OutputStream zip(String zipFileName, File inputFile) throws Exception     
59.    {     
60.        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(     
61.                zipFileName));     
62.        out.setEncoding("GBK");//解决linux乱码      
63.        zip(out, inputFile, "");     
64.        out.close();     
65.        return out;     
66.    }     
67.    
68.    private static void zip(ZipOutputStream out, File f, String base) throws Exception     
69.    {     
70.        if (f.isDirectory())     
71.        { // 判断是否为目录      
72.            File[] fl = f.listFiles();     
73.            // out.putNextEntry(new org.apache.tools.zip.ZipEntry(base + "/"));      
74.//            out.putNextEntry(new ZipEntry(base + "/"));      
75.            ZipEntry zipEntry=new ZipEntry(base + System.getProperties().getProperty("file.separator"));     
76.            zipEntry.setUnixMode(755);//解决linux乱码      
77.            out.putNextEntry(zipEntry);     
78.//            base = base.length() == 0 ? "" : base + "/";      
79.            base = base.length() == 0 ? "" : base + System.getProperties().getProperty("file.separator");     
80.            for (int i = 0; i < fl.length; i++)     
81.            {     
82.                zip(out, fl[i], base + fl[i].getName());     
83.            }     
84.        }     
85.        else    
86.        { // 压缩目录中的所有文件      
87.            // out.putNextEntry(new org.apache.tools.zip.ZipEntry(base));      
88.            ZipEntry zipEntry=new ZipEntry(base);     
89.            zipEntry.setUnixMode(644);//解决linux乱码      
90.            out.putNextEntry(zipEntry);     
91.            FileInputStream in = new FileInputStream(f);     
92.            int b;     
93.            while ((b = in.read()) != -1)     
94.            {     
95.                out.write(b);     
96.            }     
97.            in.close();     
98.        }     
99.    }     
100.}    
101.  
102.import java.io.File;  
103.import java.io.FileInputStream;  
104.