JAVA文件压缩、解压

ZIP,是一个计算机文件的压缩的算法,原名Deflate(真空),发明者为菲利普·卡兹(Phil Katz)),他于1989年1月公布了该格式的资料。ZIP通常使用后缀名“.zip”,它的MIME格式为 application/zip 。目前,ZIP格式属于几种主流的压缩格式之一,其竞争者包括RAR格式以及开放源码的7-Zip格式。从性能上比较,RAR格式较ZIP格式压缩率较高,但是它的压缩时间远远高于Zip。而7-Zip(7z)由于提供了免费的压缩工具而逐渐在更多的领域得到应用。JAVA中对其也提供了支持,我们可以很方便的对zip文件进行操作。

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 java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;

/**
 * 文件压缩工具
 * 
 * @author jianggujin
 * 
 */
public class ZipUtils
{
   /**
    * 压缩文件
    * 
    * @param zip
    *           压缩后文件
    * @param path
    *           文件夹,为空则不添加文件夹
    * @param srcFiles
    *           压缩源文件
    * @throws IOException
    */
   public void zipFiles(File zip, String path, File... srcFiles)
         throws IOException
   {
      ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zip));
      zipFiles(out, path, srcFiles);
      out.close();
   }

   /**
    * 压缩文件
    * 
    * @param out
    *           压缩输出流
    * @param path
    *           文件夹,为空则不添加文件夹
    * @param srcFiles
    *           压缩源文件
    * @throws IOException
    */
   public void zipFiles(ZipOutputStream out, String path, File... srcFiles)
         throws IOException
   {
      if (path == null)
      {
         path = "";
      }
      path = path.replaceAll("\\*", "/");
      if (path.length() > 0 && !path.endsWith("/"))
      {
         path += "/";
      }
      byte[] buf = new byte[1024];
      for (int i = 0; i < srcFiles.length; i++)
      {
         if (srcFiles[i].isDirectory())
         {
            File[] files = srcFiles[i].listFiles();
            String srcPath = srcFiles[i].getName();
            srcPath = srcPath.replaceAll("\\*", "/");
            if (!srcPath.endsWith("/"))
            {
               srcPath += "/";
            }
            out.putNextEntry(new ZipEntry(path + srcPath));
            zipFiles(out, path + srcPath, files);
         }
         else
         {
            FileInputStream in = new FileInputStream(srcFiles[i]);
            out.putNextEntry(new ZipEntry(path + srcFiles[i].getName()));
            int len;
            while ((len = in.read(buf)) > 0)
            {
               out.write(buf, 0, len);
            }
            out.closeEntry();
            in.close();
         }
      }
   }

   /**
    * 解压到指定目录
    * 
    * @param zipPath
    * @param descDir
    * @throws IOException
    */
   public void unZipFiles(String zipPath, String descDir) throws IOException
   {
      unZipFiles(new File(zipPath), new File(descDir), null);
   }

   /**
    * 解压到指定目录
    * 
    * @param zipFile
    * @param pathFile
    * @throws IOException
    */
   public void unZipFiles(File zipFile, File pathFile) throws IOException
   {
      unZipFiles(zipFile, pathFile, null);
   }

   /**
    * 解压到指定目录
    * 
    * @param zipFile
    * @param pathFile
    * @param zipList
    *           压缩文件内路径集合
    * @throws IOException
    */
   @SuppressWarnings("rawtypes")
   public void unZipFiles(File zipFile, File pathFile, List<String> zipList)
         throws IOException
   {
      if (!pathFile.exists())
      {
         pathFile.mkdirs();
      }
      ZipFile zip = new ZipFile(zipFile);
      for (Enumeration entries = zip.entries(); entries.hasMoreElements();)
      {
         ZipEntry entry = (ZipEntry) entries.nextElement();
         String zipEntryName = entry.getName();
         InputStream in = zip.getInputStream(entry);
         String outPath = zipEntryName.replaceAll("\\*", "/");
         int index = outPath.lastIndexOf('/');
         if (index != -1)
         {
            File file = new File(pathFile, outPath.substring(0,
                  outPath.lastIndexOf('/')));
            if (!file.exists())
            {
               file.mkdirs();
            }
         }
         // 判断文件全路径是否为文件夹,如果是上面已经上传,不需要解压
         File realFile = new File(pathFile, outPath);
         if (zipList != null)
         {
            zipList.add(outPath);
         }
         if (realFile.isDirectory())
         {
            continue;
         }

         OutputStream out = new FileOutputStream(realFile);
         byte[] buf1 = new byte[1024];
         int len;
         while ((len = in.read(buf1)) > 0)
         {
            out.write(buf1, 0, len);
         }
         in.close();
         out.close();
      }
   }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值