1

我可以解压缩从网上读取的 zip 文件的第一个和第二个条目,但随后出现 MalformedInputException 错误。zip 文件由 mp3 文件的 unicode 文件名组成。我使用 Winzip 创建了放在网络上的 zip 文件(我尝试了 v11 和 V18)。

mp3 文件都在 zip 文件中的“根”级别,即不存储在子文件夹中。

我首先尝试使用 ZipInputStream。最后一次尝试(如下)是使用 ArchiveInputStream。(我注意到 ArchiveInputStream 没有像 ZipInputStream 这样的 closeEntry() 方法——并不是说它有任何区别)。

错误总是发生在获取下一个条目的行上。
while ((entry = (ZipArchiveEntry)zipStream.getNextEntry()) != null)

代码是

    private void unizpMediaFile(String mediaDirectory, String zipFileURL) {
       InputStream inputStream = null;
       ArchiveInputStream zipStream = null;
       ArchiveEntry entry = null;
       try {
        // make sure can write to (probably) sd card
        File mediaFileDirectory = createMediaDirectory(mediaDirectory);
        if (mediaFileDirectory == null)
            return;
        inputStream = getHttpInputStream(zipFileURL);
        if (inputStream == null) {
            return;
        }
        zipStream =  new ArchiveStreamFactory().createArchiveInputStream(ArchiveStreamFactory.ZIP,new BufferedInputStream(
                inputStream)   );
        while ((entry = (ZipArchiveEntry)zipStream.getNextEntry()) != null) {
            Log.i(TAG,"Entry:" + entry.getName());
            if (entry.isDirectory()) {
                if (false == new File( mediaFileDirectory.getAbsoluteFile()
                        + File.separator + entry.getName()).mkdirs()) {
                    return;
                }
            } else {
                OutputStream out = new FileOutputStream(mediaFileDirectory.getAbsoluteFile()
                 + File.separator + entry.getName());
                int size;
                byte[] buffer = new byte[4096];
                FileOutputStream outStream = new FileOutputStream(
                         mediaFileDirectory.getAbsoluteFile()
                         + File.separator + entry.getName());
                BufferedOutputStream bufferOut = new BufferedOutputStream(
                        outStream, buffer.length);

                while ((size = zipStream.read(buffer, 0, buffer.length)) != -1) {
                    bufferOut.write(buffer, 0, size);
                }
                bufferOut.flush();
                bufferOut.close();
                out.close();
                Log.i(TAG,"Entry:" + entry.getName() + " closed.");
            }
        }
        maintOpDetails.append(res.getString(R.string.load_complete));
        updateLoadDetails(maintOpDetails.toString() );
    } catch (FileNotFoundException e) {
        Log.e(TAG, "unizpMediaFile" + e.toString());
        storeErrorMessage(res.getString(R.string.error_reading_media_zip_file,
                zipFileURL, e.toString()));
    } catch (IOException e) {
        Log.e(TAG, "unizpMediaFile" + e.toString());
        storeErrorMessage(res.getString(R.string.error_reading_media_zip_file,
                zipFileURL, e.toString()));
    } catch (ArchiveException e) {
        Log.e(TAG, "unizpMediaFile" + e.toString());
        storeErrorMessage(res.getString(R.string.error_reading_media_zip_file,
                zipFileURL, e.toString()));
    }
    finally {
        if (inputStream != null){ try {inputStream.close();} catch (Exception e){} }
        if (zipStream != null){ try {zipStream.close();} catch (Exception e){} }
    }
}

private InputStream getHttpInputStream(String url) {
    HttpResponse response;
    InputStream is = null;
    DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpGet httppost = new HttpGet("http://" + url);
    try {
        response = httpClient.execute(httppost);
        HttpEntity ht = response.getEntity();
        BufferedHttpEntity buf;
        buf = new BufferedHttpEntity(ht);
        is = buf.getContent();
    } catch (ClientProtocolException e) {
        Log.e(TAG, "getHttpInputStream" + e.toString());
        storeErrorMessage(res.getString(R.string.error_reading_file_at_url,
                url, e.toString()));
    } catch (ConnectTimeoutException cte) {
        Log.e(TAG, "getHttpInputStream" + cte.toString());
        storeErrorMessage(res.getString(R.string.connect_timetout_error, url));

    } catch (IOException e) {
        Log.e(TAG, "getHttpInputStream" + e.toString());
        storeErrorMessage(res.getString(R.string.error_reading_file_at_url,
                url, e.toString()));
    }
    return is;
}

从我得到的日志

I/LoadLanguageLessonService(3102): Entry:evet.mp3
I/LoadLanguageLessonService(3102): Entry:evet.mp3 closed.
I/LoadLanguageLessonService(3102): Entry:hay?r.mp3
I/LoadLanguageLessonService(3102): Entry:hay?r.mp3 closed.
E/LoadLanguageLessonService(3102): unizpMediaFilejava.nio.charset.MalformedInputException: Length: 1

(从日志剪切/粘贴到此处导致 unicode 文件名值转换为“?”您在上面看到。)

我没有运气检查了各种 SO 帖子。

有任何想法吗?

一些跟进

我修改了我的代码,首先将 zip 文件下载到我的手机,然后从那里解压缩。这样做也没有运气。

我还使用了以下代码

 ZipFile zipFile = null;
    try {
         zipFile = new ZipFile(zipFilename);
        Enumeration<?> enu = zipFile.entries();
        while (enu.hasMoreElements()) {
            ZipEntry zipEntry = (ZipEntry) enu.nextElement();
            String name = zipEntry.getName();
            long size = zipEntry.getSize();
            long compressedSize = zipEntry.getCompressedSize();
            Log.e(TAG, String.format("name: %-20s | size: %6d | compressed size: %6d\n", 
                name, size, compressedSize));
        }
    } catch (IOException e) {
        e.printStackTrace();
          throw e;
    }

列出 zip 文件中的所有条目,发现 unicode 字符都显示为一个黑色的小菱形,里面有一个问号(在剪切/粘贴到 SO 之后,字符只显示 ? 标记)。

我还下载了 Android 版 AndroZip 和 WinZip,并通过手机上的这两个应用程序查看了 zip 文件。unicode 文件名再次损坏。

在这一点上,我被困住了。我想我会换档,看看如何下载文件。

4

0 回答 0