windows下文件名正常,但linux下用ZipOutputStream压缩后中文文件名乱码

在windows下文件名正常,但部署linux下文件名却出现乱码
搞了一个晚上,知道是编码的问题,网上查到的大致有两种解决方面(好像都不行):
1、修改java.util.zip下对应的类。这个方法改了太麻烦了,而且ant包中的已经有现成的了,而且也没有用(可能是我没改好吧)
2、用ant提供的来代替,把import 里的java.util.zip改成org.apache.tools.zip。这种方法在window下支持中文了,但在linux下还是会乱码。

今天再回头看,只要在第二种方法的情况下为ZipOutputStream对象加一句setEncoding("gbk")就可以了。把自己的理解记下来以便大家遇到相似的问题可以查看,说的有有问题还请指出来哦
解决办法:
原因:org.apache.tools.zip.ZipOutputStream使用的默认编码是系统编码(window是gbk而linux是utf-8),在window下解压时用的是gbk,因些如果是在linux下压缩的文件,到window下解压就会出现乱码,因些在压缩时就为其指定编码为gbk
代码如下:
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;

 

/**
     * 多个指定文件压缩Zip文件/目录的方法。
     *
     * @param inputFileNames
     *            需要添加到压缩文件的文件名/目录
     * @param outputFileName
     *            压缩文件的文件名
     */
    public static boolean zipFiles(List inputFileNames, String outputFileName) {
        boolean result = true;
        try {
            ZipOutputStream out = null;
            try {
                File parent = new File(new File(outputFileName).getParent()); // 得到父文件夹
                if (!parent.exists()) {
                    parent.mkdirs();
                }
                out = new ZipOutputStream(new FileOutputStream(outputFileName));
                out.setEncoding("gbk");//指定编码为gbk,否则部署到linux下会出现乱码
                File inputFile = null;
                for (Object obj:inputFileNames) {   
                    Object[] input = (Object[])obj;
                    if(input.length != 2)
                        continue;
                    inputFile = new File((String) input[0]);
                    String fileN = (String)input[1];
                    zip(out, inputFile, fileN);
                }
                System.out.println("zip   done ");
            } catch (Exception e) {
                result = false;
            } finally {
                if (out != null)
                    out.close();
            }
        } catch (Exception e) {
            result = false;
            e.printStackTrace();
        }
        return result;
    }

    private static boolean zip(ZipOutputStream out, File file, String base) {
        boolean result = true;
        try {
            if (file.isDirectory()) {
                File[] subfiles = file.listFiles();
                if (base.length() != 0) {
                    out.putNextEntry(new ZipEntry(base + File.separator));
                    base += File.separator;
                }

                for (int i = 0; i < subfiles.length; i++) {
                    zip(out, subfiles[i], base + subfiles[i].getName());
                }
            } else {
                String filename = base;//java.net.URLEncoder.encode(base,"UTF-8");
                out.putNextEntry(new ZipEntry(filename));
                FileInputStream in = new FileInputStream(file);
                int b;
                System.out.println(filename);
                try {
                    while ((b = in.read()) != -1) {
                        out.write(b);
                    }
                } catch (Exception e) {
                    result = false;
                } finally {
                    in.close();
                }
            }
        } catch (Exception e) {
            result = false;
        }
        return result;
    }

 

你可能感兴趣的:(linux,windows,exception,String,object,File)