Java压缩解压缩zip文件

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.List;

public class ZipUtil {

    public static void main(String[] args) {
        List<File> list = new ArrayList<File>();
        list.add(new File("d:/无标题.JPG"));
        list.add(new File("d:/各个模块截止时间.txt"));
        list.add(new File("d:/FF276"));
       
        zip(list.toArray(new File[0]), new File("d:/test.zip"));
    }
   
    public static void unzip(String unzipFile, String savePath) {
        decompression(unzipFile, savePath);
    }
   
    public static void zip(File[] srcFiles, File destFile) {
        compression(srcFiles, destFile);
    }
   
    public static void compression(File[] srcFiles, File destFile) {
        org.apache.tools.zip.ZipOutputStream zipOut = null;
        try {
            zipOut = new org.apache.tools.zip.ZipOutputStream(new FileOutputStream(destFile));

            zipOut.setEncoding("GBK");
            System.out.println("encode === " + zipOut.getEncoding());
            for (File file : srcFiles) {
                process(zipOut, file, "");
            }
       
            zipOut.close();
        } catch(IOException e) {
            e.printStackTrace();
        } finally {
            if (zipOut != null) try {zipOut.close();} catch(Exception e) {}
        }
    }
   
    private static void process(org.apache.tools.zip.ZipOutputStream zipOut,
            File src, String path) throws IOException {
        org.apache.tools.zip.ZipEntry entry = null;
        if (src.isDirectory()) {
            path = path + src.getName() + File.separator;
            for (File file : src.listFiles()) {
                process(zipOut, file, path);
            }
        } else {
            entry = new org.apache.tools.zip.ZipEntry(path + src.getName());
            zipOut.putNextEntry(entry);
            FileInputStream fis = new FileInputStream(src);
            byte[] buf = new byte[4 * 1024];
            int len = 0;
            while ((len = fis.read(buf)) > 0) {
                zipOut.write(buf, 0, len);
            }
            fis.close();
        }
    }

    public static void decompression(String fileName, String savePath) {
        org.apache.tools.zip.ZipFile zipFile = null;
        org.apache.tools.zip.ZipEntry entry = null;
        try {
            File sourceFile = new File(fileName);
            zipFile = new org.apache.tools.zip.ZipFile(sourceFile, "GBK");
            byte[] buffer = new byte[4 * 1024];

            java.util.Enumeration<org.apache.tools.zip.ZipEntry> e = zipFile.getEntries();
            while (e.hasMoreElements()) {
                entry = e.nextElement();
                String entryName = entry.getName();
                if (entry.isDirectory()) {//目录
                    System.out.println("目录: " + entryName);
                    File directory = new File(savePath, entryName);
                    directory.mkdirs();
                } else {//文件
                    System.out.println("文件: " + entryName);
                    InputStream in = null;
                    FileOutputStream fos = null;
                    try {
                        in = zipFile.getInputStream(entry);
                        File file = new File(savePath, entryName);
                        File parentFile = file.getParentFile();
                        parentFile.mkdirs();
                        System.out.println("AbsolutePath = " + parentFile.getAbsolutePath());
                        fos = new FileOutputStream(file);

                        int length = 0;
                        while ((length = in.read(buffer)) > 0) {
                            fos.write(buffer, 0, length);
                        }
                    } catch (IOException ioe) {
                        ioe.printStackTrace();
                    } finally {
                        if (in != null) try {in.close();} catch(Exception ex) {}
                        if (fos != null) try {fos.close();} catch(Exception ex) {}
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (zipFile != null) try {zipFile.close();} catch(Exception e) {}
        }
    }

}


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/onlymilan/archive/2010/03/10/5366004.aspx

你可能感兴趣的:(Java压缩解压缩zip文件)