Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6032073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T05:16:48+00:00 2026-05-23T05:16:48+00:00

I want trying to use the Deflate and Inflate classes in java.util.zip for zlib

  • 0

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 am having this error –

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)

Here is my code so far –

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

    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T05:16:49+00:00Added an answer on May 23, 2026 at 5:16 am

    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:

    • You finish the compression in the loop – after finishing, no more data can be added.
    • You don’t check how much output the deflate process produces. If you have long lines, it could be more than 1024 bytes.
    • You set input to the Inflater without setting the length a, too.

    Some more which I found:

    • You don’t close your FileOutputStream after writing (and before reading from the same file).
    • You use 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.
    • You convert from bytes to string and to bytes again without any need.
    • You create variables which you don’t use later on.

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to spawn a process using Runtime.exec. I want to use my
Trying to use Java's regexp I want to match /app, /app/.* , but not
I am trying to use Text::DocumentCollection in Perl. I want to be able to
I am trying to use Prototype and startsWith but I want to check a
I am trying to use jquery autocomplete plugin, I want it to suggest ItemCodes
I am trying to use Eval inside a IF Statement and Repeater. I want
I'm trying to use regular expression right now and I'm really confuse. I want
I'm trying to use the CSS word-wrap property with break-word value. I want to
I'm trying to use asp: <asp:TextBox ID=txtInput runat=server TextMode=MultiLine></asp:TextBox> I want a way to
I'm trying to use preg_match to extract info from href=domain.com/subdir/?key=value The info I want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.