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

}

测试用到的文件

 

 

 

 

时间: 2014-06-04

java.util.zip.ZipOutputStream压缩无乱码(原创)的相关文章

java.util.zip.Deflater 压缩 inflater解压 实例

原文:java压缩解压缩类实例[转]   package com.example.helloworld; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.zip.Deflater; import java.util.zip.Inflater; /** * ZLib压缩工具 * * @author 梁栋 * @version 1.0 * @since 1.0 */ public a

java.util.zip - Recreating directory structure(转)

  include my own version for your reference. We use this one to zip up photos to download so it works with various unzip programs. It preserves the directory structure and timestamps. public static void createZipFile(File srcDir, OutputStream out, bo

使用java.util.zip实现文件压缩和解压

import java.io.*; import java.util.zip.*; /** *//** *功能:zip压缩.解压 *说明:本程序通过ZipOutputStream和ZipInputStream实现了zip压缩和解压功能. *问题:由于java.util.zip包并不支持汉字,当zip文件中有名字为中文的文件时 , author by http://www.bt285.cn http://www.5a520.cn * 就会出现异常:"Exception in thread &quo

java.util.zip创建和读取zip文件的类

写了一个用java.util.zip创建和读取zip文件的类 跟大家分享一下 里面用了递归调用 呵呵 近期用了不少递归调用!有空总结一下! /** TestZip.java coding by Serol Luo. rollingpig@163.com 2003/07/03 http://www.chinaunix.net/forum/viewforum.php?f=26 转载请保留此信息 */ import java.util.*; import java.util.zip.*; import

Java.util.zip adding a new file overwrites entire jar?(转)

  ZIP and TAR fomats (and the old AR format) allow file append without a full rewrite. However: The Java archive classes DO NOT support this mode of operation. File append is likely to result in multiple copies of a file in the archive if you append

我的Android进阶之旅------>Android编译错误java.util.zip.ZipException: duplicate entry的解决方法

今天在Android Studio中把另外一个项目引入当前项目,编译的时候出现了java.util.zip.ZipException: duplicate entry错误. 错误如下所示: FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ':watch:packageAllDebugClassesForMultiDex'. > java.util.zip.ZipExcept

: java.util.zip.ZipException: duplicate entry: android/support/multidex/MultiDex$V14.class

问题描述 Error:Execution failed for task ':app:transformClassesWithJarMergingForBaiduDebug'.> com.android.build.api.transform.TransformException: java.util.zip.ZipException: duplicate entry: android/support/multidex/MultiDex$V14.class 解决方案 http://www.itn

项目移到linux环境下时tomcat报错 java.util.zip.ZipException: invalid END header

问题描述 我把我的一个windows环境下的项目移到linux环境下时tomcat报错,报错如下:java.util.zip.ZipException: invalid END header (bad central directory offset)at java.util.zip.ZipFile.open(Native Method)at java.util.zip.ZipFile.<init>(ZipFile.java:114)at java.util.jar.JarFile.<i

【POI】解析xls报错:java.util.zip.ZipException: error in opening zip file

今天使用POI解析XLS,报错如下: Servlet.service() for servlet [rest] in context with path [/cetBrand] threw exception [Request processing failed; nested exception is org.apache.poi.openxml4j.exceptions.InvalidOperationException: Can't open the specified file: 'd: