ZipEntry实现文件解压缩与目录解压缩

在使用zip格式压缩、解压缩时, 压缩可以使用ZipEntry类和ZipOutPutStream类;解压缩可以使用ZipEntry类和ZipInPutStream类

ZipEntry对象:“/”结尾,则为文件夹;否则为文件。

package com.web.test;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

public class TestZiP {


    /*
     * 文件压缩
     * files:要压缩的文件,以文件名数组的形式传参
     * destZip:压缩之后zip文件所存在的目录,需要"D:\\"或"D:\\test\"格式
     * */
    public static void zip(String[] files,String destZip){

        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(destZip);
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        }
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        ZipOutputStream zos = new ZipOutputStream(bos);

        /*循环读取文件,压缩到zip中*/
        BufferedInputStream bis = null;
        FileInputStream fis = null;
        try {
            for (int i = 0; i < files.length; i++) {

                String file = files[i];

                fis = new FileInputStream(file);
                bis = new BufferedInputStream(fis);
                /*获取文件名,创建条目并添加到zip中*/
                File f = new File(file);
                ZipEntry z1 = new ZipEntry(f.getName());
                zos.putNextEntry(z1);
                /*读取文件中的字节信息,压入条目*/
                int tmp = -1;
                while((tmp = bis.read()) != -1){
                    /*写入到目标zip中*/
                    zos.write(tmp);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            try {
                zos.closeEntry();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                bis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }   
            try {
                zos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
    }   

    /*
     * 文件解压缩
     * zipfile:要解压缩的zip文件
     * destpath:解压后文件所放的目录,需要"D:\\"或"D:\\test\"格式
     * */
    public static void unzip(String zipfile,String destpath) throws FileNotFoundException,IOException{
        /*
        String  zipfile = "C:\\Users\\shitb\\Desktop\\jacob_1.9.zip";
        String destpath = "D:\\test\\";
        */
        FileInputStream fis = new FileInputStream(zipfile);
        ZipInputStream zis = new ZipInputStream(fis);
        ZipEntry z1 = null;

        while((z1 = zis.getNextEntry()) != null){
            if (z1.isDirectory()) {
                File f = new File("D:\\"+z1.getName());
                f.mkdirs();
            }else {
                String fileName = z1.getName();
                FileOutputStream fos = new FileOutputStream(destpath+fileName);
                int tmp = -1;
                while((tmp = zis.read()) != -1){
                    /*写入到目标文件中*/
                    fos.write(tmp);
                }
                zis.closeEntry();
                fos.close();
            }
        }
        zis.close();
    }

    /*
     * 目录压缩
     * zipFileName:压缩后zip文件的路径
     * inputFile:需要压缩的源目录;
     * */
    public void zip(String zipFileName,String inputFile) throws IOException{

        File file = new File(inputFile);

        FileOutputStream fos = new FileOutputStream(zipFileName);
        ZipOutputStream out = new ZipOutputStream(fos);
        zipDir(out,file,"");
        System.out.println("ZIP done..");
        out.close();
    }
    /*
     *递归遍历子目录和文件,进行压缩
     * */
    private void zipDir(ZipOutputStream out, File file, String base) throws IOException {

        System.out.println("Zipping:"+file.getName());
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            out.putNextEntry(new ZipEntry(base+"/"));
            base = base.length()==0?"":base+"/";
            for (int i = 0; i < files.length; i++) {
                /*进行递归*/
                zipDir(out,files[i], base+files[i].getName());
            }
        }else {
            out.putNextEntry(new ZipEntry(base));
            FileInputStream fis = new FileInputStream(file);

            int len = 0;
            while((len = fis.read()) != -1){
                /*写入到目标zip中*/
                out.write(len);
            }
            fis.close();
        }
    }
    /*
     * 解压缩目录
     * zipFileName:需要解压缩的zip文件
     * outputDirectory:解压缩后文件所放路径
     * */
    public void unzipDir(String zipFileName,String outputDirectory) throws IOException{

        FileInputStream fis = new FileInputStream(zipFileName);
        ZipInputStream zis = new ZipInputStream(fis);

        ZipEntry zipEntry;

        while((zipEntry = zis.getNextEntry()) != null){
            System.out.println("unzipping:"+zipEntry.getName());
            if (zipEntry.isDirectory()) {
                String name = zipEntry.getName();
                name = name.substring(0,name.length()-1);
                File f = new File(outputDirectory+File.separator+name);
                f.mkdir();
                System.out.println("mkdir:"+outputDirectory+File.separator+name);
            }else {
                File f = new File(outputDirectory+File.separator+zipEntry.getName());
                f.createNewFile();
                FileOutputStream fos = new FileOutputStream(f);

                int len = 0;
                while((len = zis.read()) != -1){
                    /*写入到目标文件中*/
                    fos.write(len);
                }
                fos.close();
            }
        }
        zis.close();
    }
}

你可能感兴趣的:(JavaSE-File)