利用zip压缩和解压文件(目录)

package com.yonge.zip;

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

import org.apache.commons.io.IOUtils;

/**
 * 压缩目标文件为zip文件
 * @author wb-gaoy
 * @version $Id: CompressByZip.java,v 0.1 2013-1-8 上午10:18:16 wb-gaoy Exp $
 */
public class ZipUtil {

    public static void zip(File srcFile, File targetFile) throws IOException {
        ZipOutputStream zipOutputStream = null;
        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = new FileOutputStream(targetFile);
            zipOutputStream = new ZipOutputStream(fileOutputStream);
            zip(zipOutputStream, srcFile, "");
        } finally {
            if (zipOutputStream != null) {
                zipOutputStream.close();
            }
            if (fileOutputStream != null) {
                fileOutputStream.close();
            }
        }
    }

    public static void zip(ZipOutputStream zipOutputStream, File srcFile, String baseURL)
                                                                                         throws IOException {

        baseURL = baseURL == null || baseURL.length() == 0 ? srcFile.getName() : baseURL
                                                                                 + "/"
                                                                                 + srcFile
                                                                                     .getName();
        if (srcFile.isDirectory()) {
            File[] files = srcFile.listFiles();
            if (files != null && files.length > 0) {
                for (File f : files) {
                    zip(zipOutputStream, f, baseURL);
                }
            } else {
                zipOutputStream.putNextEntry(new ZipEntry(baseURL + "/"));
            }
        } else {
            zipOutputStream.putNextEntry(new ZipEntry(baseURL));

            FileInputStream fileInputStream = null;
            try {
                //读数据
                fileInputStream = new FileInputStream(srcFile);
                byte[] bytes = new byte[1024];
                int length = -1;
                while ((length = fileInputStream.read(bytes)) != -1) {
                    zipOutputStream.write(bytes, 0, length);
                }
            } finally {
                if (fileInputStream != null) {
                    fileInputStream.close();
                }
            }
        }
    }

    public static void unzip(File srcFile, File targetFile) throws IOException {
        FileInputStream fileInputStream = null;
        ZipInputStream zipInputStream = null;
        FileOutputStream fileOutputStream = null;
        try {
            fileInputStream = new FileInputStream(srcFile);
            zipInputStream = new ZipInputStream(fileInputStream);
            if (!targetFile.exists()) {
                targetFile.mkdirs();
            }
            ZipEntry entry = zipInputStream.getNextEntry();
            File tempFile;
            while (entry != null) {
                tempFile = new File(targetFile, entry.getName());
                if (entry.isDirectory()) {
                    tempFile.mkdir();
                } else {
                    tempFile.getParentFile().mkdirs();
                    fileOutputStream = new FileOutputStream(tempFile);
                    IOUtils.copy(zipInputStream, fileOutputStream);
                }
                entry = zipInputStream.getNextEntry();
            }
        } finally {
            if (zipInputStream != null) {
                zipInputStream.close();
            }
            if (fileInputStream != null) {
                fileInputStream.close();
            }
            if (fileOutputStream != null) {
                fileOutputStream.close();
            }
        }
    }

    public static void main(String[] args) {
        File file = new File("C:\\ecmng\\site\\tmp\\test");
        try {
            //zip(file, new File("demo.zip"));
            unzip(new File("demo.zip"), file);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值