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

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();
    }

}

测试用到的文件

 

 

 

 

posted @ 2014-06-04 23:32  沧海一滴  阅读(5880)  评论(0编辑  收藏  举报