0

当我解压缩档案时,我在 Java 中收到一条奇怪的错误消息。奇怪的是,我在两台不同的服务器上运行完全相同的代码,使用完全相同的文件,一台服务器抛出异常,而另一台服务器按预期工作。

基本上,有一个 Java 小程序向 PHP 服务器发送一个 zip 文件的请求。两台服务器之间的一个区别是工作的一台运行 PHP 5.2,而损坏的一台运行 PHP 5.3。我不确定 PHP 5.3 是否无法正确发送 ZIP 文件或什么...

我真的不太了解 zip 文件。因此,我们将非常非常感谢任何帮助。

zip 文件在一个 Java 类 (DownloadZipFile.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)
4

0 回答 0