2011-05-30 10 views
8

io voglio cercando di utilizzare lo Sgonfiare e gonfiare le classi in java.util.zip per la compressione zlib.compressione zlib Utilizzando Sgonfiare e gonfiare le classi in Java

sono in grado di comprimere il codice utilizzando Sgonfiare, ma durante la decompressione, sto avendo questo errore -

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) 

Ecco il mio codice finora -

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

è che un compito? un errore sta chiamando finitura troppo presto, un altro sta usando setInput senza lunghezza e un altro non sta controllando se il processo di svuotamento ha restituito più dati rispetto al 1024. – bestsss

risposta

25

Cosa stai cercando di fare qui? Usi un InflaterInputStream, che decomprime i tuoi dati, e poi provi a trasferire nuovamente questi dati decompressi a un Inflater? Utilizzare uno di essi, ma non entrambi.

Questo è ciò che sta causando la vostra un'eccezione qui.

Oltre a questo, ci sono un bel alcuni piccoli errori, come queste di cui parla bestsss:

  • aver terminato la compressione nel ciclo - dopo aver terminato, non più dati possono essere aggiunti.
  • Non si controlla la quantità di output prodotta dal processo di svuotamento. Se hai linee lunghe, potrebbe essere più di 1024 byte.
  • si imposta ingresso al Inflater senza impostare la lunghezza a, anche.

Alcuni più che ho trovato:

  • non si chiude la tua FileOutputStream dopo la scrittura (e prima di leggere dallo stesso file).
  • si utilizza readLine() a leggere una riga di testo, ma poi non si aggiunge l'interruzione di riga di nuovo, il che significa che nel file decompresso non sarà eventuali interruzioni di riga.
  • Si converte di nuovo da byte in stringa e in byte senza alcuna necessità.
  • Si creano variabili che non verranno utilizzate in seguito.

Non voglio cercare di correggere il vostro programma. Ecco una semplice che fa quello che penso che tu voglia, usando DeflaterOutputStream e InflaterInputStream. (Si potrebbe anche usare ZInputStream e ZOutputStream di JZlib invece.)

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

Per una maggiore efficienza, potrebbe essere utile per avvolgere i flussi di file con i flussi buffer. Se questo è un aspetto critico, misuralo. Codice

+0

sei sicuro che questo è per zlib. Provo a decomprimere un file zlib ma ottengo un errore 'metodo di compressione sconosciuto'. Se guardo http://docs.oracle.com/javase/1.4.2/docs/api/java/util/zip/package-summary.html allora vedo 'Inflater' menzionare zlib ma si usa' InflaterInputStream' che menziona il metodo di compressione deflate. Questo è il file: https://dl.dropboxusercontent.com/u/17630770/temp/pc_oj_simple_AnimTake1_11.icecache.zip – clankill3r

+0

InflaterInputStream utilizza (se si utilizza il costruttore senza il parametro Inflater) un 'nuovo Inflater()'. E questo costruttore usa 'nowrap = false', che significa la compressione zlib. (Non ho controllato il tuo file.) –

4

Paŭlo Ebermann 's può essere ulteriormente migliorata utilizzando 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

Qual è lo scopo dello scanner? Stai ricevendo la prima riga del flusso? –

+1

@ Paŭlo Ebermann: Questo è un trucco per leggere l'intero stream in una sola volta – BullyWiiPlaza