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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T14:49:02+00:00 2026-05-10T14:49:02+00:00

I am trying to read a single file from a java.util.zip.ZipInputStream , and copy

  • 0

I am trying to read a single file from a java.util.zip.ZipInputStream, and copy it into a java.io.ByteArrayOutputStream (so that I can then create a java.io.ByteArrayInputStream and hand that to a 3rd party library that will end up closing the stream, and I don’t want my ZipInputStream getting closed).

I’m probably missing something basic here, but I never enter the while loop here:

ByteArrayOutputStream streamBuilder = new ByteArrayOutputStream(); int bytesRead; byte[] tempBuffer = new byte[8192*2]; try {     while ((bytesRead = zipStream.read(tempBuffer)) != -1) {         streamBuilder.write(tempBuffer, 0, bytesRead);     } } catch (IOException e) {     // ... } 

What am I missing that will allow me to copy the stream?

Edit:

I should have mentioned earlier that this ZipInputStream is not coming from a file, so I don’t think I can use a ZipFile. It is coming from a file uploaded through a servlet.

Also, I have already called getNextEntry() on the ZipInputStream before getting to this snippet of code. If I don’t try copying the file into another InputStream (via the OutputStream mentioned above), and just pass the ZipInputStream to my 3rd party library, the library closes the stream, and I can’t do anything more, like dealing with the remaining files in the stream.

  • 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. 2026-05-10T14:49:03+00:00Added an answer on May 10, 2026 at 2:49 pm

    Your loop looks valid – what does the following code (just on it’s own) return?

    zipStream.read(tempBuffer) 

    if it’s returning -1, then the zipStream is closed before you get it, and all bets are off. It’s time to use your debugger and make sure what’s being passed to you is actually valid.

    When you call getNextEntry(), does it return a value, and is the data in the entry meaningful (i.e. does getCompressedSize() return a valid value)? IF you are just reading a Zip file that doesn’t have read-ahead zip entries embedded, then ZipInputStream isn’t going to work for you.

    Some useful tidbits about the Zip format:

    Each file embedded in a zip file has a header. This header can contain useful information (such as the compressed length of the stream, it’s offset in the file, CRC) – or it can contain some magic values that basically say ‘The information isn’t in the stream header, you have to check the Zip post-amble’.

    Each zip file then has a table that is attached to the end of the file that contains all of the zip entries, along with the real data. The table at the end is mandatory, and the values in it must be correct. In contrast, the values embedded in the stream do not have to be provided.

    If you use ZipFile, it reads the table at the end of the zip. If you use ZipInputStream, I suspect that getNextEntry() attempts to use the entries embedded in the stream. If those values aren’t specified, then ZipInputStream has no idea how long the stream might be. The inflate algorithm is self terminating (you actually don’t need to know the uncompressed length of the output stream in order to fully recover the output), but it’s possible that the Java version of this reader doesn’t handle this situation very well.

    I will say that it’s fairly unusual to have a servlet returning a ZipInputStream (it’s much more common to receive an inflatorInputStream if you are going to be receiving compressed content.

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

Sidebar

Ask A Question

Stats

  • Questions 89k
  • Answers 90k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Try using a GridBagLayout and adding the panel with an… May 11, 2026 at 5:59 pm
  • Editorial Team
    Editorial Team added an answer url() is part of the value of background-image, so I… May 11, 2026 at 5:59 pm
  • Editorial Team
    Editorial Team added an answer If you're trying to set the text to the text… May 11, 2026 at 5:59 pm

Related Questions

I'm trying to read a 16-bit greyscale TIFF file (BitsPerSample=16) using a small C
I have several log files of events (one event per line). The logs can
I am trying to create a databound WPF GridView whose rows can either be
I am trying to use the ASP.NET Dynamic Data features to generate CRUD scaffolding

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.