I'm getting a bizarre error message in Java when I go to unzip an archive. The weird thing is that I'm running the exact same code on two different servers, with the exact same file, and one server throws an exception while the other one works as expected.
Basically, there's a Java applet that sends a request to the PHP server for a zip file. One difference between the two servers is that the working one runs PHP 5.2 while the broken one runs PHP 5.3. I'm not sure if PHP 5.3 won't send a ZIP file properly or what...
I really don't know all that much about zip files. So any help would be very, very much appreciated.
The zip file gets downloaded in one Java class (DownloadZipFile.java) and then unzipped in another (UnzipFiles.java), and the two are linked together via the Apache chain of command library. I'm also using the Apache HTTPClient and HTTPCore libraries to make the actual HTTP request and unpack the HTTP response.
Here's the code that gets and later extracts the file:
// DownloadZipFile.java
/*
* (2) The HTTP response should be the (zip) file that we're requesting.
*/
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity( );
entityLength = entity.getContentLength( );
if(entityLength == 0) {
throw new org.apache.http.ConnectionClosedException("Disconnected! " +
"Please check your internet connection and try again.");
}
/*
* We want to copy the HTTP response (i.e., the file from the server) into a temporary
* file on the local machine.
*
* (a) ...so, first create a temporary file on the local machine.
*/
try {
zipFile = File.createTempFile("if_", ".zip", new File(System.getProperty("java.io.tmpdir")));
zipFile.deleteOnExit( );
}
catch(IOException e) { /* ... */ }
/*
* (b) Get an InputStream from the HTTP response and get an OutputStream to the temporary
* file we just created.
*/
if(entity != null) {
InputStream responsein = entity.getContent( );
OutputStream responseout = new FileOutputStream(zipFile);
}
if(!httppost.isAborted( )) {
/*
* (c) Copy the contents of the InputStream to the OutputStream.
*/
try {
IOUtils.copy(responsein, responseout);
}
catch(IOException e) { ... }
context.setZipFile(zipFile);
/* ... */
// UnzipFiles.java
public class UnzipFiles implements Command {
public final String UPDATE_PATH = "php/record_download.php?PHPSESSID=";
/* (non-Javadoc)
* @see org.apache.commons.chain.Command#execute(org.apache.commons.chain.Context)
*/
public boolean execute(Context ctx) throws Exception {
/*
* (1) We need to wrap a ZipFile object around the file we downloaded, or else we won't be able
* to unzip it.
*/
ZipFile zipFile = new ZipFile(context.getZipFile( )); // throws an exception
...
The error is:
java.util.zip.ZipException: archive is not a ZIP archive
at org.apache.commons.compress.archivers.zip.ZipFile.positionAtCentralDirectory32(ZipFile.java:716)
at org.apache.commons.compress.archivers.zip.ZipFile.positionAtCentralDirectory(ZipFile.java:671)
at org.apache.commons.compress.archivers.zip.ZipFile.populateFromCentralDirectory(ZipFile.java:405)
at org.apache.commons.compress.archivers.zip.ZipFile.<init>(ZipFile.java:205)
at org.apache.commons.compress.archivers.zip.ZipFile.<init>(ZipFile.java:181)
at org.apache.commons.compress.archivers.zip.ZipFile.<init>(ZipFile.java:142)
at com.kenjackson.UnzipFiles.execute(UnzipFiles.java:45)
at org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:191)
at com.kenjackson.IFApplet$1.doInBackground(IFApplet.java:78)
at com.kenjackson.IFApplet$1.doInBackground(IFApplet.java:1)
at javax.swing.SwingWorker$1.call(SwingWorker.java:296)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
at java.util.concurrent.FutureTask.run(FutureTask.java:166)
at javax.swing.SwingWorker.run(SwingWorker.java:335)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:722)