java压缩zip文件中文乱码问题 linux 下

来源:互联网 发布:招行信用卡 知乎 编辑:程序博客网 时间:2024/05/24 05:12
ant.jar 
import org.apache.tools.zip.ZipEntry;import org.apache.tools.zip.ZipOutputStream;
public void workZips(String zipName,String fileName,List<File> fileList) throws Exception{byte[] buffer = new byte[1024];   String filepath = ServletActionContext.getServletContext().getRealPath("file") +"/"+memberId+ "/zip/";File directory = new File(filepath);if (!directory.exists()) {FileUtils.forceMkdir(directory);}//生成的ZIP文件名为Demo.zip   ZipOutputStream out = new ZipOutputStream(new FileOutputStream(filepath+fileName+".zip"));   out.setEncoding("gbk");//需要同时下载的两个文件result.txt ,source.txt   for(int i=0;i<fileList.size();i++) {   FileInputStream fis = new FileInputStream(filepath+fileList.get(i).toString().split(";")[1]+".zip");out.putNextEntry(new ZipEntry(fileList.get(i).toString().split(";")[1]+".zip"));    int len;    //读入需要下载的文件的内容,打包到zip文件     while((len = fis.read(buffer))>0) {      out.write(buffer,0,len);           }          out.closeEntry();           fis.close();       //删除被合并的zip        this.deleteFile(filepath+fileList.get(i).toString().split(";")[1]+".zip");   }         out.close();   }
out.setEncoding("gbk"); 这里很关键,你要是不设置的话,默认获取系统编码;这样设置,在linux下压缩zip,在window系统解压就不会出现乱码了
</pre><pre code_snippet_id="422359" snippet_file_name="blog_20140708_6_3093008" name="code" class="java">注:我们写文件的时候使用UTF-8的编码来写文件名的,这个时候-- 奇迹出现了“乱码”,为什么呢? GBK每个汉字占2个字节,而UTF-8每个汉字占3个字节,它们所占字节数都不一样,乱码是必须的


0 0