2011-05-30 86 views
8

我想嘗試使用java.util.zip中的Deflate和Inflate類進行zlib壓縮。Zlib壓縮在Java中使用Deflate和Inflate類

我能夠壓縮採用放氣的代碼,但在解壓縮,我有這個錯誤 -

Exception in thread "main" java.util.zip.DataFormatException: unknown compression method 
    at java.util.zip.Inflater.inflateBytes(Native Method) 
    at java.util.zip.Inflater.inflate(Inflater.java:238) 
    at java.util.zip.Inflater.inflate(Inflater.java:256) 
    at zlibCompression.main(zlibCompression.java:53) 

這裏是我到目前爲止的代碼 -

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

public class zlibCompression { 

    /** 
    * @param args 
    */ 
    public static void main(String[] args) throws IOException, DataFormatException { 
     // TODO Auto-generated method stub 

     String fname = "book1"; 
     FileReader infile = new FileReader(fname); 
     BufferedReader in = new BufferedReader(infile); 

     FileOutputStream out = new FileOutputStream("book1out.dfl"); 
     //BufferedInputStream in = new BufferedInputStream(new FileInputStream(filename)); 

     Deflater compress = new Deflater(); 
     Inflater decompress = new Inflater(); 

     String readFile = in.readLine(); 
     byte[] bx = readFile.getBytes(); 

     while(readFile!=null){ 
      byte[] input = readFile.getBytes(); 
      byte[] compressedData = new byte[1024]; 
      compress.setInput(input); 
      compress.finish(); 
      int compressLength = compress.deflate(compressedData, 0, compressedData.length); 
      //System.out.println(compressedData); 
      out.write(compressedData, 0, compressLength); 
      readFile = in.readLine(); 
     } 

     File abc = new File("book1out.dfl"); 
     InputStream is = new FileInputStream("book1out.dfl"); 

     InflaterInputStream infl = new InflaterInputStream(new FileInputStream("book1out.dfl"), new Inflater()); 
     FileOutputStream outFile = new FileOutputStream("decompressed.txt"); 

     byte[] b = new byte[1024]; 
     while(true){ 

      int a = infl.read(b,0,1024); 
      if(a==0) 
       break; 

      decompress.setInput(b); 
      byte[] fresult = new byte[1024]; 
      //decompress.in 
      int resLength = decompress.inflate(fresult); 
      //outFile.write(b,0,1); 
      //String outt = new String(fresult, 0, resLength); 
      //System.out.println(outt); 
     } 

     System.out.println("complete"); 

    } 
} 
+0

是一個家庭作業?一個錯誤是過早調用finish,另一個錯誤是使用setInput w/o長度,另外一個錯誤是檢查deflate過程是否返回了比1024更多的數據。 – bestsss 2011-05-30 15:09:50

回答

25

什麼是你想在這裏做?您使用InflaterInputStream來解壓縮數據,然後嘗試再次將解壓縮的數據傳遞給Inflater?使用其中之一,但不能同時使用兩者。

這是什麼導致你的異常在這裏。

除此之外,還有相當一些小錯誤,像這樣的由bestsss提到:

  • 您完成循環壓縮 - 完成後,沒有更多的數據可以被添加。
  • 您不檢查放氣過程產生多少輸出。如果你有很長的行,它可能會超過1024字節。
  • 您也可以在不設定長度a的情況下爲Inflater設置輸入。

,我發現一些更多:

  • 你不寫後(並從同一文件讀取前)關閉FileOutputStream中。
  • 您使用readLine()來讀取一行文本,但不會再次添加換行符,這意味着在解壓縮的文件中不會有任何換行符。
  • 您可以將字節轉換爲字符串並再次轉換爲字節,而無需任何需要。
  • 您可以創建稍後不使用的變量。

我不會嘗試更正您的程序。這是一個簡單的,它使用DeflaterOutputStream和InflaterInputStream來做我想做的事。 (你也可以使用JZlib的ZInputStream和ZOutputStream代替。)

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

/** 
* Example program to demonstrate how to use zlib compression with 
* Java. 
* Inspired by http://stackoverflow.com/q/6173920/600500. 
*/ 
public class ZlibCompression { 

    /** 
    * Compresses a file with zlib compression. 
    */ 
    public static void compressFile(File raw, File compressed) 
     throws IOException 
    { 
     InputStream in = new FileInputStream(raw); 
     OutputStream out = 
      new DeflaterOutputStream(new FileOutputStream(compressed)); 
     shovelInToOut(in, out); 
     in.close(); 
     out.close(); 
    } 

    /** 
    * Decompresses a zlib compressed file. 
    */ 
    public static void decompressFile(File compressed, File raw) 
     throws IOException 
    { 
     InputStream in = 
      new InflaterInputStream(new FileInputStream(compressed)); 
     OutputStream out = new FileOutputStream(raw); 
     shovelInToOut(in, out); 
     in.close(); 
     out.close(); 
    } 

    /** 
    * Shovels all data from an input stream to an output stream. 
    */ 
    private static void shovelInToOut(InputStream in, OutputStream out) 
     throws IOException 
    { 
     byte[] buffer = new byte[1000]; 
     int len; 
     while((len = in.read(buffer)) > 0) { 
      out.write(buffer, 0, len); 
     } 
    } 


    /** 
    * Main method to test it all. 
    */ 
    public static void main(String[] args) throws IOException, DataFormatException { 
     File compressed = new File("book1out.dfl"); 
     compressFile(new File("book1"), compressed); 
     decompressFile(compressed, new File("decompressed.txt")); 
    } 
} 

爲了更高的效率,它可能是有用的與緩衝流包裹文件流。如果這對性能至關重要,那麼測量它。

+0

您確定這是zlib。我嘗試解壓縮一個zlib文件,但是我得到了一個'未知的壓縮方法'錯誤。如果我查看http://docs.oracle.com/javase/1.4.2/docs/api/java/util/zip/package-summary.html,那麼我會看到'Inflater'提到zlib,但您使用'InflaterInputStream'提到deflate壓縮方法。 這是文件: https://dl.dropboxusercontent.com/u/17630770/temp/pc_oj_simple_AnimTake1_11。icecache.zip – clankill3r 2013-06-11 20:34:02

+0

InflaterInputStream使用(如果使用沒有Inflater參數的構造函數)'新的Inflater()'。這個構造函數使用'nowrap = false',這意味着zlib壓縮。 (我沒有檢查你的文件。) – 2013-06-13 04:59:22

4

Paŭlo Ebermann的代碼可以通過使用try-with-resources可以進一步提高:

import java.util.Scanner; 
import java.util.zip.*; 
import java.io.*; 

public class ZLibCompression 
{ 
    public static void compress(File raw, File compressed) throws IOException 
    { 
     try (InputStream inputStream = new FileInputStream(raw); 
      OutputStream outputStream = new DeflaterOutputStream(new FileOutputStream(compressed))) 
     { 
      copy(inputStream, outputStream); 
     } 
    } 

    public static void decompress(File compressed, File raw) 
      throws IOException 
    { 
     try (InputStream inputStream = new InflaterInputStream(new FileInputStream(compressed)); 
      OutputStream outputStream = new FileOutputStream(raw)) 
     { 
      copy(inputStream, outputStream); 
     } 
    } 

    public static String decompress(File compressed) throws IOException 
    { 
     try (InputStream inputStream = new InflaterInputStream(new FileInputStream(compressed))) 
     { 
      return toString(inputStream); 
     } 
    } 

    private static String toString(InputStream inputStream) 
    { 
     try (Scanner scanner = new Scanner(inputStream).useDelimiter("\\A")) 
     { 
      return scanner.hasNext() ? scanner.next() : ""; 
     } 
    } 

    private static void copy(InputStream inputStream, OutputStream outputStream) 
      throws IOException 
    { 
     byte[] buffer = new byte[1000]; 
     int length; 

     while ((length = inputStream.read(buffer)) > 0) 
     { 
      outputStream.write(buffer, 0, length); 
     } 
    } 
} 
+0

掃描儀的目標是什麼?只需獲取流的第一行? – 2017-07-20 13:07:22

+1

@PaŭloEbermann: 這是一次讀取整個流的技巧 – BullyWiiPlaza 2017-07-20 13:10:10