java实现zip文件压缩,解压

来源:互联网 发布:杭州淘宝摄影工作室 编辑:程序博客网 时间:2024/05/05 08:43

这几天看了网上一些前辈的代码,自己对Java实现zip文件的解压,压缩有一点理解,故写下留着以后参考。

为了处理中文乱码问题,使用ant.jar包。

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream;

public class TestZip {
  
    private static int readByte;

    private static byte[] buf = new byte[1024];

    private static ZipOutputStream zipOut;

    private static BufferedInputStream fileIn;

    private static BufferedOutputStream fileOut;
   
    private static InputStream in;
   
    private static ZipFile zipFile;
   
    /**
     * 遍历数组,调用文件压缩方法,压缩文件
     *
     * @param list 文件集合
     * @param zipName 压缩有的文件名
     * @return zipFile 压缩后的文件
     * @throws IOException
     */
    public static File zip(List<File> list, String zipName) throws IOException {
        File zipFile = new File(zipName+".zip");
        zipOut = new ZipOutputStream(new BufferedOutputStream(
                new FileOutputStream(zipFile)));
        //将字符集设定为GBK,支持中文。
        zipOut.setEncoding("GBK");
        for (File file : list) {
            //获取文件的父文件路径,便于递归压缩文件夹。
            String path = file.getParent();
            utilZip(file, zipOut, path);
        }
        zipOut.close();
        return zipFile;
    }
/**
 * 通过递归实现文件夹以及文件的压缩
 *
 * @param fileDir 
 * @param zipOutput
 * @param path 文件的路径
 * @throws IOException
 */
    public static void utilZip(File fileDir, ZipOutputStream zipOutput,
            String path) throws IOException {
        //文件为文件夹
        if (fileDir.isDirectory()) {
            zipOutput.putNextEntry(new ZipEntry(fileDir.getPath().substring(
                    path.length() + 1)
                    + "/"));
            zipOut.closeEntry();
            for (File file : fileDir.listFiles()) {
                //递归调用
                utilZip(file, zipOutput, path);
            }

        } else {
            //文件数据传输
            fileIn = new BufferedInputStream(new FileInputStream(fileDir));
            zipOutput.putNextEntry(new ZipEntry(fileDir.getPath().substring(
                    path.length())));
            while ((readByte = fileIn.read(buf)) > 0) {
                zipOutput.write(buf, 0, readByte);
            }
            //关闭流
            fileIn.close();
            zipOut.closeEntry();
        }
    }

    /**
     * 遍历文件,如果有文件夹,创建文件夹,如果为文件,复制文件。
     * @param zipFileName 被解压的文件名
     * @return list 解压后的文件集合
     * @throws IOException
     */
    @SuppressWarnings("unchecked")
    public static List<File> unZip(File zipFileName) throws IOException{
        List<File> list = new ArrayList<File>();
         zipFile = new ZipFile(zipFileName);
         Enumeration en = zipFile.getEntries();
         //获取被解压文件的父文件路径
         String path = zipFileName.getParent();
         ZipEntry zipEntry = null;
         while(en.hasMoreElements()){
             zipEntry = (ZipEntry) en.nextElement();
             //文件夹
             if(zipEntry.isDirectory()){
                 //创建文件夹
                 File file = new File(path+"//"+zipEntry.getName());
                 file.mkdirs();
             }else{
                 //复制文件
                 File file = new File(path+"//"+zipEntry.getName());
                 in =zipFile.getInputStream(zipEntry);
                 fileOut = new BufferedOutputStream(new FileOutputStream(file));
                 while((readByte = in.read(buf)) > 0 ){
                     fileOut.write(buf, 0, readByte);
                 }
                 in.close();
                 fileOut.close();
                 list.add(file);
             }
         }
         zipFile.close();
         
        return list;
    }
}