java - 还是个问题? - java.util.zip.ZipException : Central Directory Entry not found

标签 java android encoding archive

作为免责声明,我已经看过 this帖子和链接到它的帖子。

我在服务器上托管了一个文件,该文件是 n 个归档文件,我正尝试使用 this 取消归档它。方法。当文件被预先下载到设备上并且我在我的应用程序中打开并取消存档时,通过 Downloads 中的 intent-filter,没有任何问题。但是,当我从我的应用程序中的服务器下载它,然后尝试解压缩它时,我在这一行的标题中收到错误消息:

ZipFile zipfile = new ZipFile(archive);

其中 archive 是一个 File 指向我下载的存档文件。我用来下载存档的代码如下:

    String urlPath = parameters[0], localPath = parameters[1];

    try
    {
        URL url = new URL(urlPath);
        URLConnection connection = url.openConnection();
        connection.addRequestProperty("Accept-Encoding", "gzip");
        connection.connect();

        int fileLength = connection.getContentLength();

        InputStream input = new BufferedInputStream(url.openStream());
        OutputStream output = new BufferedOutputStream(new FileOutputStream(localPath));

        byte data[] = new byte[1024];
        long total = 0;
        int count;

        while((count = input.read(data)) != -1)
        {
            total += count;
            publishProgress((int)total * 100 / fileLength);
            output.write(data);
        }

        output.flush();
        output.close();
        input.close();

我最近根据我在顶部引用的帖子添加了编码类型,但我仍然遇到相同的错误。任何帮助都会很棒。

澄清一下:

  • 我有一个存档文件
  • 当文件从外部下载并在我的应用程序中打开/取消存档时,它可以正常取消存档
  • 下载存档然后尝试取消存档时,我收到错误 java.util.zip.ZipException: Central Directory Entry not found

我最好的猜测是这是我下载的问题。但是,话虽如此,我不知道自己做错了什么。

最佳答案

您没有正确复制下载内容。你必须使用

output.write(data, 0, count);

否则你就是在向文件中写入任意垃圾。

关于java - 还是个问题? - java.util.zip.ZipException : Central Directory Entry not found,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14655689/

相关文章:

ios - NSString 使用编码将字符串转换为 NSData

java - 调用 super 方法是否有意义?

java - Java中如何使用OpenGL监控GPU使用情况?

java - 在jsp中读取Active Directory用户名

java - PL/SQL : Inserting the values of a column of a table into an array or VARRAY to be accessed by java?

java - android 上的 libcurl CURLE_SSL_CACERT_BADFILE 错误

Android RelativeLayout UI 显示不正确

android - 使用按钮在运行时调整 ImageView 的大小 - Android

perl - 如何对电子邮件地址中的特殊字符进行编码

ruby - 尝试使用 Ruby 1.8/FasterCSV 解析带有变音符号等的 CSV 文件时出现问题