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

  • Home
  • SEARCH
  • 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 7662005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:41:33+00:00 2026-05-31T13:41:33+00:00

I’m trying to take a stream of data from standard in, compress it one

  • 0

I’m trying to take a stream of data from standard in, compress it one 128 byte block at a time, and then output it to standard out. (Example: “cat file.txt | java Dict | gzip -d | cmp file.txt”, where file.txt just contains some ASCII characters.)

I also need to use a 32 byte dictionary taken from the end of each previous 128 byte block, for each subsequent block. (The first block uses its own first 32 bytes as its dictionary.) When I don’t set the dictionary at all, the compression works fine. However, when I do set the dictionary, gzip gives me an error trying to decompress the data: “gzip: stdin: invalid compressed data–crc error”.

I’ve tried adding/changing several parts of the code, but nothing has worked so far, and I haven’t had any luck finding solutions with Google.

I’ve tried…

  • Adding “def.reset()” before “def.setDictionary(b)” near the bottom of the code does not work.
  • Only setting the dictionary for blocks after the first block does not work. (Not using a dictionary for the first block.)
  • Calling updateCRC with the “input” array before or after compressor.write(input, 0, bytesRead) does not work.

I’d really appreciate any suggestions – is there anything obvious I’m missing or doing wrong?

This is what I have in my Dict.java file:

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

public class Dict {
  protected static final int BLOCK_SIZE = 128;
  protected static final int DICT_SIZE = 32;

  public static void main(String[] args) {
    InputStream stdinBytes = System.in;
    byte[] input = new byte[BLOCK_SIZE];
    byte[] dict = new byte[DICT_SIZE];
    int bytesRead = 0;

    try {
        DictGZIPOuputStream compressor = new DictGZIPOuputStream(System.out);
        bytesRead = stdinBytes.read(input, 0, BLOCK_SIZE);
        if (bytesRead >= DICT_SIZE) {
            System.arraycopy(input, 0, dict, 0, DICT_SIZE);
            compressor.setDictionary(dict);
        }

        do {
            compressor.write(input, 0, bytesRead);
            compressor.flush();

            if (bytesRead == BLOCK_SIZE) {
                System.arraycopy(input, BLOCK_SIZE-DICT_SIZE-1, dict, 0, DICT_SIZE);
                compressor.setDictionary(dict);
            }
            bytesRead = stdinBytes.read(input, 0, BLOCK_SIZE);
        } while (bytesRead > 0);

        compressor.finish();
    }
    catch (IOException e) {e.printStackTrace();}
  }

  public static class DictGZIPOuputStream extends GZIPOutputStream {
    public DictGZIPOuputStream(OutputStream out) throws IOException {
        super(out);
    }

    public void setDictionary(byte[] b) {
        def.setDictionary(b);
    }
    public void updateCRC(byte[] input) {
        crc.update(input);
    }
  }
}
  • 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-31T13:41:35+00:00Added an answer on May 31, 2026 at 1:41 pm

    I do not know exactly internally zlib algorithm work but based on my understanding on DictGZIPOutputStream, when you call write() method, after it is write, it will update its crc for that byte array. So if you call again updateCRC() in your code again, then thing become wrong as the crc is updated twice. Then when gzip -d is executed, as a result of previous two crc updates, gzip will complaint “invalid compressed data–crc error”

    I also noticed that you did not close the compressor after it is used. When I executed the code pasted above, it gave error “gzip: stdin: unexpected end of file”. So always make sure to flush method and close method is called in the end. With that said, I have the following,

    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.util.zip.GZIPOutputStream;
    
    
    public class Dict
    {
        protected static final int BLOCK_SIZE = 128;
        protected static final int DICT_DIZE = 32;
    
        public static void main(String[] args)
        {
            InputStream stdinBytes = System.in;
            byte[] input = new byte[BLOCK_SIZE];
            byte[] dict = new byte[DICT_DIZE];
            int bytesRead = 0;
    
            try
            {
                DictGZIPOutputStream compressor = new DictGZIPOutputStream(System.out);
                bytesRead = stdinBytes.read(input, 0, BLOCK_SIZE);
    
                if (bytesRead >= DICT_DIZE)
                {
                    System.arraycopy(input, 0, dict, 0, DICT_DIZE);
                }
    
                do 
                {               
                    compressor.write(input, 0, bytesRead);              
    
                    if (bytesRead == BLOCK_SIZE)
                    {
                        System.arraycopy(input, BLOCK_SIZE-1, dict, 0, DICT_DIZE);
                        compressor.setDictionary(dict);
                    }
    
                    bytesRead = stdinBytes.read(input, 0, BLOCK_SIZE);
                }
                while (bytesRead > 0);
                compressor.flush();         
                compressor.close();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
    
        }
    
        public static class DictGZIPOutputStream extends GZIPOutputStream
        {
    
            public DictGZIPOutputStream(OutputStream out) throws IOException
            {
                super(out);
            }
    
            public void setDictionary(byte[] b)
            {
                def.setDictionary(b);
            }
    
            public void updateCRC(byte[] input)
            {
                crc.update(input);
            }                       
        }
    
    }
    

    The test result at the console.

    $ cat file.txt 
    hello world, how are you?1e3djw
    hello world, how are you?1e3djw adfa asdfas
    
    $ cat file.txt | java Dict | gzip -d | cmp file.txt ; echo $?
    0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to select an H1 element which is the second-child in its group

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.