当我解压缩存档时,我在Java中收到一条奇怪的错误消息。奇怪的是,我在两个不同的服务器上运行完全相同的代码,使用完全相同的文件,一个服务器抛出异常,而另一个服务器按预期工作。
基本上,有一个Java applet向PHP服务器发送一个zip文件请求。两台服务器之间的一个区别是工作的服务器运行PHP 5.2,而损坏的服务器运行PHP 5.3。我不确定PHP 5.3是否不能正确发送ZIP文件或者是什么......
我真的不太了解zip文件。所以任何帮助都会非常非常感激。
将zip文件下载到一个Java类(DownloadZipFile.java)中,然后在另一个Java类(UnzipFiles.java)中解压缩,并通过Apache链命令库将它们链接在一起。我还使用Apache HTTPClient和HTTPCore库来发出实际的HTTP请求并解压缩HTTP响应。
以下是获取并稍后提取文件的代码:
// 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
...
错误是:
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)