首页 > 编程语言 > 详细

java.util.zip.ZipOutputStream压缩无乱码(原创)

时间:2014-06-05 14:57:24      阅读:419      评论:0      收藏:0      [点我收藏+]
bubuko.com,布布扣
package io;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.zip.Adler32;
import java.util.zip.CheckedOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class ZipCompress {

    /**
     * @param args
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        
        String[] fileNames={"config.properties","中文.properties","中文.csv"};
        FileOutputStream f=new FileOutputStream("test.zip");
        
        CheckedOutputStream csum=new CheckedOutputStream(f, new Adler32());
        
        ZipOutputStream zos=new ZipOutputStream(csum);
        
        BufferedOutputStream out=new BufferedOutputStream(zos);
        
        zos.setComment("comment");
        for (String fileName : fileNames) {
            BufferedReader in=new BufferedReader(new InputStreamReader(new FileInputStream(fileName),"GBK"));
            zos.putNextEntry(new ZipEntry(fileName));
            
            String temp;
            while((temp=in.readLine())!=null){
                out.write(temp.getBytes("GBK"));
            }
            
            in.close();
            out.flush();
        }
        out.close();
    }

}
bubuko.com,布布扣

测试用到的文件

 

 

 

 

java.util.zip.ZipOutputStream压缩无乱码(原创),布布扣,bubuko.com

java.util.zip.ZipOutputStream压缩无乱码(原创)

原文:http://www.cnblogs.com/softidea/p/3768992.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!