ITPub博客

首页 > 应用开发 > Java > Java使用Zip压缩

Java使用Zip压缩

原创 Java 作者:壹頁書 时间:2014-05-12 00:38:53 0 删除 编辑
使用压缩流需要注意的是
压缩输入流的available方法返回的永远是1或者0


  1. import java.util.zip.*;
  2. import java.io.*;

  3. public class ZipUtil {
  4.     public static void main(String[] args) throws IOException {
  5.         unzip("/home/lihuilin/桌面/src.zip", "/home/lihuilin/桌面/tar/");
  6.         zip("/home/lihuilin/桌面/tar/", "/home/lihuilin/桌面/target.zip");
  7.     }

  8.     public static void unzip(String source, String target) throws IOException {
  9.         File t = new File(target);
  10.         if (!t.exists()) {
  11.             t.mkdirs();
  12.         }
  13.         ZipInputStream in = new ZipInputStream(new BufferedInputStream(new FileInputStream(source)));
  14.         String filepath = target;
  15.         ZipEntry entry = null;
  16.         int b;
  17.         while ((entry = in.getNextEntry()) != null) {
  18.             if (entry.isDirectory()) {
  19.                 new File(filepath + entry.getName()).mkdirs();
  20.             } else {
  21.                 System.out.println(filepath + entry.getName());
  22.                 BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(filepath + entry.getName()));
  23.                 while ((b = in.read()) != -1) {
  24.                     out.write(b);
  25.                 }
  26.                 in.closeEntry();
  27.                 out.flush();
  28.                 out.close();
  29.             }
  30.         }
  31.         in.close();

  32.     }

  33.     public static void zip(String source, String target) throws IOException {
  34.         ZipOutputStream out = new ZipOutputStream(new FileOutputStream(target));
  35.         for (File file : new File(source).listFiles()) {
  36.             zip(file, out, source);
  37.         }
  38.         out.close();

  39.     }

  40.     private static void zip(File file, ZipOutputStream out, String filepath) throws IOException {

  41.         if (file.isDirectory()) {
  42.             for (File f : file.listFiles()) {
  43.                 zip(f, out, filepath);
  44.             }
  45.         } else {
  46.             ZipEntry entry = new ZipEntry(file.getPath().replaceAll(filepath, ""));
  47.             out.putNextEntry(entry);

  48.             FileInputStream fis = new FileInputStream(file.toString());
  49.             int i = 0;
  50.             while ((i = fis.read()) != -1) {
  51.                 out.write(i);
  52.             }
  53.             fis.close();
  54.             out.flush();
  55.             out.closeEntry();
  56.         }

  57.     }

  58. }

使用GZIP压缩

  1. import java.io.BufferedInputStream;
  2. import java.io.BufferedOutputStream;
  3. import java.io.FileInputStream;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.util.zip.GZIPInputStream;
  7. import java.util.zip.GZIPOutputStream;

  8. public class GZIPUtil {
  9.     public static void main(String[] args) throws IOException {
  10.         compress("C:\\Users\\new\\Desktop\\源文件.txt", "C:\\Users\\new\\Desktop\\目標文件.txt");
  11.         decompress("C:\\Users\\new\\Desktop\\目標文件.txt", "C:\\Users\\new\\Desktop\\解压文件.txt");
  12.     }

  13.     private static void decompress(String source, String target) throws IOException {
  14.         GZIPInputStream in = new GZIPInputStream(new BufferedInputStream(new FileInputStream(source)));
  15.         BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(target));
  16.         int b = 0;
  17.         while ((b = in.read()) != -1) {
  18.             out.write(b);
  19.         }
  20.         out.flush();
  21.         out.close();
  22.         in.close();
  23.     }

  24.     private static void compress(String source, String target) throws IOException {
  25.         BufferedInputStream bis = new BufferedInputStream(new FileInputStream(source));
  26.         GZIPOutputStream out = new GZIPOutputStream(new BufferedOutputStream(new FileOutputStream(target)));
  27.         byte[] data = new byte[bis.available()];
  28.         bis.read(data);
  29.         out.write(data);
  30.         out.flush();
  31.         out.close();
  32.         bis.close();
  33.     }
  34. }


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29254281/viewspace-1158736/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: 我在后勤那三年
请登录后发表评论 登录
全部评论

注册时间:2013-10-19

  • 博文量
    620
  • 访问量
    6339970