[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1128
  • Last Modified:

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

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.

Thanks
// Retrieving the file
// Create a URL for the desired page
                java.net.URL url = new java.net.URL( sControlFileURL + sURL );
                // Read all the text returned by the server
                BufferedReader in = new BufferedReader( new InputStreamReader( url.openStream() ) );
                FileOutputStream file = new FileOutputStream( sDomain + "\\" + "tmp" + "\\" + "ListFile.zip" );
                BufferedOutputStream out = new BufferedOutputStream( file );
                int i;
                while ((i = in.read()) != -1) {
                    out.write( i );
                }
                out.flush();
                in.close();
                //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
                        i++;
                        // unmarshal message from zip file
                        otdSureScripts_PharmacyList_1.unmarshalFromString( zipReader.readLine() );

Open in new window

0
cindymccartney
Asked:
cindymccartney
  • 2
  • 2
  • 2
  • +1
3 Solutions
 
CPColinCommented:
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?
0
 
CEHJCommented:
Yes you *must* use an InputStream, not a Reader. The latter is for text files
0
 
cindymccartneyAuthor Commented:
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\a99.zip (The system cannot find the path specified)"

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

"/Downloads/a99.zip"

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

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
CPColinCommented:
Try this:

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

Open in new window

0
 
objectsCommented:
following shows how to read a stream from a url

http://helpdesk.objects.com.au/java/how-to-write-get-response-to-byte-array

and use the following to copy the stream to your FileOutputStream

http://helpdesk.objects.com.au/java/how-do-i-copy-one-stream-to-another-using-java

let me know if you have any questions or problems

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

should be:

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

0
 
cindymccartneyAuthor Commented:
Thanks everyone for the help, got it working using the BufferedInputStream.  
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now