Java中文件的压缩

今天项目中用了下 java.util.zip 中的 ZipOutputStream 用来压缩文件,发现存在中文乱码问题,查了半天发现它不能很好的支持中文,所以用truezip来代替,在网上找到用法如下:

import java.io.File;   
import java.io.IOException;   
  
import org.apache.commons.io.CopyUtils;   
  
import de.schlichtherle.io.FileInputStream;   
import de.schlichtherle.io.FileOutputStream;   
import de.schlichtherle.util.zip.ZipEntry;   
import de.schlichtherle.util.zip.ZipOutputStream;   
  
public class Test   
{   
    public static void main(String []args); throws Exception   
    {   
        String needtozipfilepath ="";   
        String zipfilepath = "";   
        File needtozipfile = new File(needtozipfilepath);;   
        File zipfile = new File(zipfilepath);;   
        FileOutputStream fout = new FileOutputStream(zipfile);;   
        ZipOutputStream zout = new ZipOutputStream(fout,"GBK");;   //解决中文问题的关键所在   
        try  
        {   
            for(File in : needtozipfile.listFiles(););   
            {   
                ZipEntry ze = new ZipEntry(in.getName(););;   
                zout.putNextEntry(ze);;   
                FileInputStream fis = new FileInputStream(in);;   
                try  
                {   
                    CopyUtils.copy(fis,zout);;                             
                }   
                catch (IOException e);   
                {   
                    // TODO: handle exception   
                }   
                finally  
                {   
                    if(fis != null);   
                        fis.close();;   
                    zout.closeEntry();;   
                }                  
            }   
        }   
        catch (IOException e);   
        {   
            // TODO: handle exception   
        }   
        finally  
        {   
            if(zout != null);   
            {   
                zout.close();;   
                fout.close();;   
            }   
        }   
           
    }   
}  

 

附件中有相关JAR包和源码及文档

你可能感兴趣的:(java,apache)