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 8055175
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T08:18:42+00:00 2026-06-05T08:18:42+00:00

I have a problem in code: private static String compress(String str) { String str1

  • 0

I have a problem in code:

private static String compress(String str)
{
    String str1 = null;
    ByteArrayOutputStream bos = null;
    try
    {
        bos = new ByteArrayOutputStream();
        BufferedOutputStream dest = null;

        byte b[] = str.getBytes();
        GZIPOutputStream gz = new GZIPOutputStream(bos,b.length);
        gz.write(b,0,b.length);
        bos.close();
        gz.close();

    }
    catch(Exception e) {
        System.out.println(e);
        e.printStackTrace();
    }
    byte b1[] = bos.toByteArray();
    return new String(b1);
}

private static String deCompress(String str)
{
    String s1 = null;

    try
    {
        byte b[] = str.getBytes();
        InputStream bais = new ByteArrayInputStream(b);
        GZIPInputStream gs = new GZIPInputStream(bais);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        int numBytesRead = 0;
        byte [] tempBytes = new byte[6000];
        try
        {
            while ((numBytesRead = gs.read(tempBytes, 0, tempBytes.length)) != -1)
            {
                baos.write(tempBytes, 0, numBytesRead);
            }

            s1 = new String(baos.toByteArray());
            s1= baos.toString();
        }
        catch(ZipException e)
        {
            e.printStackTrace();
        }
    }
    catch(Exception e) {
        e.printStackTrace();
    }
    return s1;
}

public String test() throws Exception
    {
        String str = "teststring";
        String cmpr = compress(str);
        String dcmpr = deCompress(cmpr);
}

This code throw java.io.IOException: unknown format (magic number ef1f)

GZIPInputStream gs = new GZIPInputStream(bais);

It turns out that when converting byte new String (b1) and the byte b [] = str.getBytes () bytes are “spoiled.” At the output of the line we have already more bytes. If you avoid the conversion to a string and work on the line with bytes – everything works. Sorry for my English.


public String unZip(String zipped) throws DataFormatException, IOException {
    byte[] bytes = zipped.getBytes("WINDOWS-1251");
    Inflater decompressed = new Inflater();
    decompressed.setInput(bytes);

    byte[] result = new byte[100];
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();

    while (decompressed.inflate(result) != 0)
        buffer.write(result);

    decompressed.end();

    return new String(buffer.toByteArray(), charset);
}

I’m use this function to decompress server responce. Thanks for help.

  • 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-06-05T08:18:45+00:00Added an answer on June 5, 2026 at 8:18 am

    You have two problems:

    • You’re using the default character encoding to convert the original string into bytes. That will vary by platform. It’s better to specify an encoding – UTF-8 is usually a good idea.
    • You’re trying to represent the opaque binary data of the result of the compression as a string by just calling the String(byte[]) constructor. That constructor is only meant for data which is encoded text… which this isn’t. You should use base64 for this. There’s a public domain base64 library which makes this easy. (Alternatively, don’t convert the compressed data to text at all – just return a byte array.)

    Fundamentally, you need to understand how different text and binary data are – when you want to convert between the two, you should do so carefully. If you want to represent “non text” binary data (i.e. bytes which aren’t the direct result of encoding text) in a string you should use something like base64 or hex. When you want to encode a string as binary data (e.g. to write some text to disk) you should carefully consider which encoding to use. If another program is going to read your data, you need to work out what encoding it expects – if you have full control over it yourself, I’d usually go for UTF-8.

    Additionally, the exception handling in your code is poor:

    • You should almost never catch Exception; catch more specific exceptions
    • You shouldn’t just catch an exception and continue as if it had never happened. If you can’t really handle the exception and still complete your method successfully, you should let the exception bubble up the stack (or possibly catch it and wrap it in a more appropriate exception type for your abstraction)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following method in some nemerle code: private static getLinks(text : string)
I have the following code: public class Events extends ListActivity { //... private static
I have a problem with my database in Android app. Here's some code: private
I have problem with this code: file = tempfile.TemporaryFile(mode='wrb') file.write(base64.b64decode(data)) file.flush() os.fsync(file) # file.seek(0)
Hi i have problem with this code, i found it on the internet and
i am a beginner and i have a problem : this code doesnt compile
I have a code problem which stems from the fact that I am using
i have a problem with my code. foreach (DataRow dr in dt_pattern.Rows) { part
I have some problem with my code public IQueryable<PageItems> GetPageById(Guid Id) { var xml
I have a problem in php code inserting values into database (I use PHPMyAdmin).

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.