doufud21086 2013-06-09 21:01
浏览 157

无法解压缩存档。 错误消息是“存档不是ZIP存档”

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)
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥50 永磁型步进电机PID算法
    • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
    • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
    • ¥15 如何处理复杂数据表格的除法运算
    • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
    • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
    • ¥200 uniapp长期运行卡死问题解决
    • ¥15 latex怎么处理论文引理引用参考文献
    • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
    • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?