2011-05-30 12 views
8

Chcę próbować użyć klas Deflate i Inflate w java.util.zip do kompresji zlib.Kompresja Zlib Używanie klas Deflate i Inflate w Javie

jestem w stanie skompresować kod za pomocą spuścić powietrze, ale podczas dekompresji, mam ten błąd -

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) 

Oto mój kod do tej pory -

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

jest to, że w pracy domowej? jednym błędem jest wywoływanie zakończenia zbyt wcześnie, inny używa metody setInput bez długości, a jeszcze jeden nie sprawdza, czy proces deflate zwrócił więcej danych niż 1024. – bestsss

Odpowiedz

25

Co ty próbujesz zrób tutaj? Używasz InflaterInputStream, który dekompresuje dane, a następnie próbujesz przekazać te zdekompresowane dane ponownie do Inflatera? Użyj jednego z nich, ale nie obu.

To właśnie jest przyczyną wyjątku.

Oprócz tego, istnieją dość pewne drobne błędy, takie jak te wymienione przez bestsss:

  • zakończeniu kompresji w pętli - po zakończeniu, nie więcej danych mogą być dodawane.
  • Nie sprawdzasz, ile danych wyjściowych generuje proces deflate. Jeśli masz długie wiersze, może to być więcej niż 1024 bajty.
  • Ustawiasz wejście do Inflater'a bez ustawiania długości a.

Niektóre bardziej który znalazłem:

  • Nie zamykać FileOutputStream po napisaniu (a przed odczytu z tego samego pliku).
  • Używasz readLine() do czytania linii tekstu, ale nie dodajesz ponownie linii, co oznacza, że ​​w twoim zdekompresowanym pliku nie będzie żadnych podziałów linii.
  • Konwersja z bajtów na ciąg i bajtów ponownie bez potrzeby.
  • Tworzysz zmienne, których później nie używasz.

Nie będę próbował poprawić programu. Oto prosty, który robi to, co myślę, że chcesz, używając DeflaterOutputStream i InflaterInputStream. (Można także użyć JZlib za ZInputStream i ZOutputStream zamiast.)

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

Dla większej efektywności, może być przydatna do owijania strumienie plików z buforowanych strumieni. Jeśli jest to krytyczne z punktu widzenia wydajności, zmierz je.

+0

czy jesteś pewien, że jest to zlib. Próbuję zdekompresować plik zlib, ale pojawia się błąd "nieznana metoda kompresji". Jeśli spojrzę na http://docs.oracle.com/javase/1.4.2/docs/api/java/util/zip/package-summary.html, wtedy zobaczę 'Inflater', wspominając zlib, ale użyjesz' InflaterInputStream' który wspomina metodę deflacji kompresji. To jest plik: https://dl.dropboxusercontent.com/u/17630770/temp/pc_oj_simple_AnimTake1_11.icecache.zip – clankill3r

+0

InflaterInputStream używa (jeśli używa się konstruktora bez parametru "Pływak") 'new Inflater()'. I ten konstruktor używa 'nowrap = false', co oznacza kompresję zlib. (Nie sprawdziłem twojego pliku.) –

4

Paŭlo Ebermann „s kod można dodatkowo poprawić za pomocą 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

Jaki jest cel skanera? Właśnie dostajesz pierwszą linię strumienia? –

+1

@ Paŭlo Ebermann: To jest sztuczka, aby przeczytać cały strumień na raz – BullyWiiPlaza