码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2014-06-05 14:57:06      阅读:233      评论:0      收藏:0      [点我收藏+]

标签:c   style   class   blog   code   java   

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压缩无乱码(原创)

标签:c   style   class   blog   code   java   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!