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();
      }
   }
}

你可能感兴趣的:(java,压缩,zip,解压)