android ZipArchiveEntry给出了java.nio.charset.MalformedInputException

时间:2014-05-01 20:58:23

标签: java android unicode zipinputstream

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

mp3文件都在' root' 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后,字符只显示?标记)。 / p>

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

此时我被困住了。我想我会换档,看看有关逐个下载的文件。

0 个答案:

没有答案