java.util.zip.ZipException:未找到中央目录条目

发布于 2024-10-31 20:02:15 字数 2455 浏览 5 评论 0原文

我有一个代码在模拟器上运行得非常好,但是当我在 Samsung Galaxy Tab 上运行它时,它给出了异常。
我通过套接字从服务器收到一个压缩的 zip 文件,然后我正在提取这些文件。如果我压缩并发送两个或三个文本文件,它在 ie 模拟器和 Galaxy Tab 上都可以正常运行。
但是,如果我压缩并发送一些带有文本或两个图像文件的小图像文件,它会给出:>java.util.zip.ZipException:未找到中央目录条目<在 Galaxy Tab 上,但在模拟器上没有错误。 Zip 文件大小不超过 32 KB,并且我确信文件被正确接收。这是我的解压缩器代码

package com.vsi.vremote;

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import android.content.Context;
import android.util.Log;

public class UnCompressor {
    private static final String TAG = "UnCompressor";
    Context context;

    public UnCompressor(Context context) {
        this.context = context;
    }

    private final void copyInputStream(InputStream in, OutputStream out)

    throws IOException {
        byte[] buffer = new byte[1024];
        int len;

        while ((len = in.read(buffer)) >= 0)
            out.write(buffer, 0, len);

        in.close();
        out.close();
    }

    public final String[] unCompress(String name) {
        try {
            Log.d(TAG, "Uncompress called");

            ZipFile zipFile = new ZipFile(context.getFileStreamPath(name));
            Log.d(TAG, "Zip file created");
            Enumeration entries = zipFile.entries();
            String fileNames[] = new String[zipFile.size()];
            int counter = 0;

            Log.d(TAG, "Loop strting");
            while (entries.hasMoreElements()) {
                Log.d(TAG, "Getting next entry");
                ZipEntry entry = (ZipEntry) entries.nextElement();

                Log.d(TAG, "Extracting file: " + entry.getName());
                copyInputStream(
                        zipFile.getInputStream(entry),
                        new BufferedOutputStream(context.openFileOutput(
                                entry.getName(), Context.MODE_PRIVATE)));
                fileNames[counter++] = entry.getName();
            }
            zipFile.close();
            return fileNames;

        } catch (IOException ioe) {
            System.err.println("Unhandled exception:");
            ioe.printStackTrace();
            return null;
        }
    }

    public final void delete(String fileName) {
        context.deleteFile(fileName);
    }
}

注意:我刚刚在我的 HTC WildFire 上检查了它,它也可以在这款手机上运行,​​但 Galaxy TAB :(

I have a code which runs absolutely fine on emulator but when I run it on Samsung Galaxy Tab, it gives Exception.
I am receiving one compressed zip file from server via socket and than I am extracting these file. If I compress and send two or three text files it runs fine on both i.e. emulator and Galaxy Tab.
But if I compress and send some small image file with text or two image files it gives: >java.util.zip.ZipException: Central Directory Entry not found < on Galaxy Tab but no error on emulator. Zip file size does not exceeds 32 KB and I am sure that file is being received correctly. Here is my uncompressor code

package com.vsi.vremote;

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import android.content.Context;
import android.util.Log;

public class UnCompressor {
    private static final String TAG = "UnCompressor";
    Context context;

    public UnCompressor(Context context) {
        this.context = context;
    }

    private final void copyInputStream(InputStream in, OutputStream out)

    throws IOException {
        byte[] buffer = new byte[1024];
        int len;

        while ((len = in.read(buffer)) >= 0)
            out.write(buffer, 0, len);

        in.close();
        out.close();
    }

    public final String[] unCompress(String name) {
        try {
            Log.d(TAG, "Uncompress called");

            ZipFile zipFile = new ZipFile(context.getFileStreamPath(name));
            Log.d(TAG, "Zip file created");
            Enumeration entries = zipFile.entries();
            String fileNames[] = new String[zipFile.size()];
            int counter = 0;

            Log.d(TAG, "Loop strting");
            while (entries.hasMoreElements()) {
                Log.d(TAG, "Getting next entry");
                ZipEntry entry = (ZipEntry) entries.nextElement();

                Log.d(TAG, "Extracting file: " + entry.getName());
                copyInputStream(
                        zipFile.getInputStream(entry),
                        new BufferedOutputStream(context.openFileOutput(
                                entry.getName(), Context.MODE_PRIVATE)));
                fileNames[counter++] = entry.getName();
            }
            zipFile.close();
            return fileNames;

        } catch (IOException ioe) {
            System.err.println("Unhandled exception:");
            ioe.printStackTrace();
            return null;
        }
    }

    public final void delete(String fileName) {
        context.deleteFile(fileName);
    }
}

Note: I just checked it on my HTC WildFire, it is also working on this mobile but galaxy TAB :(

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

☆獨立☆ 2024-11-07 20:02:15

在此处输入图像描述

它只是将这些文件添加到 zip 文件中(参见屏幕显示),但我必须从中创建一个 zip 文件那些 zip 文件部分。我只是将 zip 文件分成几个部分并上传到服务器上。现在我想在我的应用程序中下载这些文件并将 zip 文件重建到应用程序中。

enter image description here

Its only adding those file into a zip file (see screenshow) but i have to make a zip file from those zip file parts. I simply devided my zip file into parts and uploaded on the server. now i want to download these files in the my app and rebuild the zip file into the app.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文