Java 文件或者文件夹的压缩和解压

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;

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

public class FileToZip {

    /**
     * main方法
     *
     * @param temp
     */
    public static void main(String[] temp) {
        // 单个文件压缩测试
        if (zipReady("D:\\text\\1\\测试.txt", "D:\\text\\1\\aa.zip")) {
            System.out.println("success");
        } else {
            System.out.println("file");
        }
        // 文件夹压缩测试
        if (zipReady("D:\\text\\1", "D:\\text\\1.zip")) {
            System.out.println("success");
        } else {
            System.out.println("file");
        }
        // 文件夹解压缩测试
        if (unZip("D:\\text\\1.zip", "D:\\text\\dd")) {
            System.out.println("success");
        } else {
            System.out.println("file");
        }
    }

    /**
     * 生成压缩流,准备压缩
     *
     * @param fileName
     *            要压缩的文件或文件夹 如 d:/a.txt 或者 d:/a
     * @param zipFilePath
     *            生成的压缩文件路径 如 d:/a.zip
     */
    public static boolean zipReady(String fileName, String zipFilePath) {
        File file = new File(fileName);
        ZipOutputStream zos = null;
        String basePath = "";
        try {
            zos = new ZipOutputStream(new File(zipFilePath));
            zos.setEncoding("gbk");
            zip(file, zos, basePath);
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        } finally {
            if (zos != null) {
                try {
                    zos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return true;
    }

    /**
     * 将一个文件或者文件夹写入到压缩流中,即完成压缩
     *
     * @param fileName
     *            要压缩的文件或文件夹
     * @param zos
     *            压缩流
     */
    public static void zip(File fileName, ZipOutputStream zos, String basePath) {
        InputStream is = null;
        try {
            // 首先判断压缩的是一个文件夹,还是文件
            if (fileName.isDirectory()) {// 如果是一个文件夹,那么先把该文件夹压缩进去,然后递归
                basePath = basePath + fileName.getName() + "/";
                zos.putNextEntry(new ZipEntry(basePath));
                File[] files = fileName.listFiles();
                for (int i = 0; i < files.length; i++) {
                    File file = files[i];
                    // 回调
                    zip(file, zos, basePath);
                }
            } else {// 如果是一个文件就直接压缩进去,将内容写进去
                zos.putNextEntry(new ZipEntry(basePath + fileName.getName()));
                is = new FileInputStream(fileName);
                BufferedInputStream bis = new BufferedInputStream(is);
                byte[] buf = new byte[1024];
                int length = -1;
                while ((length = bis.read(buf)) != -1) {
                    zos.write(buf, 0, length);
                    zos.flush();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 解压一个文件
     *
     * @param zipfilename
     *            解压的文件
     * @param destDir
     *            解压的目录
     */
    public static boolean unZip(String zipfilename, String destDir) {
        File file = new File(zipfilename);
        OutputStream os = null;
        InputStream is = null;
        if (!file.isFile() || !file.getName().endsWith(".zip")) {
            System.out.println("该程序无法解压非zip文件");
            return false;
        } else {
            destDir = destDir.endsWith("\\") ? destDir : destDir + "\\";
            byte b[] = new byte[1024];
            int length;
            ZipFile zipFile;
            try {
                zipFile = new ZipFile(file, "gbk");
                Enumeration enumeration = zipFile.getEntries();
                ZipEntry zipEntry = null;
                while (enumeration.hasMoreElements()) {
                    zipEntry = (ZipEntry) enumeration.nextElement();
                    File loadFile = new File(destDir + zipEntry.getName());
                    // 判断压缩文件中的某个条目是文件夹还是文件
                    if (zipEntry.isDirectory()) {// 如果是目录,那么判断该文件是否已存在并且不是一个文件夹,解决空文件夹解压后不存在的问题
                        if (!loadFile.exists()) {
                            loadFile.mkdirs();
                        }
                    } else {
                        if (!loadFile.getParentFile().exists()) {
                            loadFile.getParentFile().mkdirs();
                        }
                        os = new FileOutputStream(loadFile);
                        is = zipFile.getInputStream(zipEntry);
                        while ((length = is.read(b)) > 0) {
                            os.write(b, 0, length);
                            os.flush();
                        }
                    }

                }
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            } finally {
                try {
                    if (is != null) {
                        is.close();
                    }
                    if (os != null) {
                        os.close();
                    }
                } catch (Exception e2) {
                    e2.printStackTrace();
                }
            }
        }
        return true;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值