Java使用Zip壓縮

壹頁書發表於2014-05-12
使用壓縮流需要注意的是
壓縮輸入流的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部落格 ” ,連結:http://blog.itpub.net/29254281/viewspace-1158736/,如需轉載,請註明出處,否則將追究法律責任。

相關文章