Unable to read zip file from windows, but works fine in Unix.

Posted on 2009-04-27
Last Modified: 2012-05-06
I am using the following Java code to download a .zip file from a URL.  I then write the file out to a directory so I can extract and read the file.  This code works fine when run on a Unix machine (AIX), but when I moved it to a Windows box, I am getting errors extracting the file.  I am not even able to open the file using winzip, as the following error occurs:

WinZip Error: " invalid compressed data to expand (inflate) the file"

The java error is a little different:

"incomplete dynamic bit lengths tree"

I have pasted my java code below.

// Retrieving the file

// Create a URL for the desired page

       url = new sControlFileURL + sURL );

                // Read all the text returned by the server

                BufferedReader in = new BufferedReader( new InputStreamReader( url.openStream() ) );

                FileOutputStream file = new FileOutputStream( sDomain + "\\" + "tmp" + "\\" + "" );

                BufferedOutputStream out = new BufferedOutputStream( file );

                int i;

                while ((i = != -1) {

                    out.write( i );




                //zip file download complete

// Reading the file after download

            // ZipFile offers an Enumeration of all the files in the Zip file

            zipFile = new ZipFile( fileName );

            for (Enumeration e = zipFile.entries(); e.hasMoreElements();) {

                ZipEntry zipEntry = (ZipEntry) e.nextElement();

                log( 0, sDesc + "Uncompressed name of file: " + zipEntry.getName(), runMain );

                // use BufferedReader to get one line at a time

                BufferedReader zipReader = new BufferedReader( new InputStreamReader( zipFile.getInputStream( zipEntry ) ) );

                log( 0, sDesc + "Begin Reading file line by line", runMain );

                while (zipReader.ready()) {

                    RUN runMessage = new RUN();

                    String sRunMessageID = (new java.rmi.server.UID()).toString() + "-" + i;

                    try {

                        // count records processed


                        // unmarshal message from zip file

                        otdSureScripts_PharmacyList_1.unmarshalFromString( zipReader.readLine() );

Open in new window

Question by:cindymccartney
    LVL 14

    Accepted Solution

    I'll bet it has something to do with the BufferedReader and InputStreamReader you're using. Those translate the data they read according to "the default charset," which is almost certainly different between the platforms. Maybe try using a BufferedInputStream instead of the two readers?
    LVL 86

    Expert Comment

    Yes you *must* use an InputStream, not a Reader. The latter is for text files

    Author Comment

    Ok, I switched to using this line instead of the InputStreamReader, but the problem here is I don't have the file.  I need to retrieve the file from a URL.  

    ZipInputStream in = new ZipInputStream( new FileInputStream( url.getFile() ) );

    The above compiles, but it returns this error:

    "\Downloads\ (The system cannot find the path specified)"

    However, when I write the path out to a log, I get this:


    Are we sure it's possible to use a FileInputStream from a URL?

    LVL 14

    Assisted Solution

    Try this:

    BufferedInputStream in = new BufferedInputStream(url.openStream());

    Open in new window

    LVL 92

    Assisted Solution

    following shows how to read a stream from a url

    and use the following to copy the stream to your FileOutputStream

    let me know if you have any questions or problems

    LVL 92

    Expert Comment

    > ZipInputStream in = new ZipInputStream( new FileInputStream( url.getFile() ) );

    should be:

    ZipInputStream in = new ZipInputStream(url.openStream() );


    Author Closing Comment

    Thanks everyone for the help, got it working using the BufferedInputStream.  

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    how to extract window origin in spring 2 55
    withoutTen challenge 14 69
    array6 challenfge 6 49
    endX challenge 2 33
    For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
    Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
    Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now