I want trying to use the Deflate and Inflate classes in java.util.zip for zlib compression.
I am able to compress the code using Deflate, but while decompressing, I
What are you trying to do here? You use an InflaterInputStream, which decompresses your data, and then you try to pass this decompressed data again to an Inflater? Use either one of them, but not both.
This is what is causing your exception here.
In addition to this, there are quite some minor errors, like these mentioned by bestsss:
a
, too.Some more which I found:
readLine()
to read a line of text, but then you don't add the line break again, which means in your decompressed file won't be any line breaks.I won't try to correct your program. Here is a simple one which does what I think you want, using DeflaterOutputStream and InflaterInputStream. (You could also use JZlib's ZInputStream and ZOutputStream instead.)
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"));
}
}
For more efficiency, it might be useful to wrap the file streams with buffered streams. If this is performance critical, measure it.
Paŭlo Ebermann's code can be further improved by using 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);
}
}
}