JAVA zip解壓 MALFORMED 錯誤

來源:互聯網
上載者:User

標籤:

最近在在使用zip 解壓時,使用JDK1.7及以上版本在解壓時,某些檔案會報異常

Exception in thread "main" java.lang.IllegalArgumentException: MALFORMED at java.util.zip.ZipCoder.toString(ZipCoder.java:58) at java.util.zip.ZipFile.getZipEntry(ZipFile.java:567) at java.util.zip.ZipFile.access$900(ZipFile.java:61) at java.util.zip.ZipFile$ZipEntryIterator.next(ZipFile.java:525) at java.util.zip.ZipFile$ZipEntryIterator.nextElement(ZipFile.java:500) at java.util.zip.ZipFile$ZipEntryIterator.nextElement(ZipFile.java:481) at zip.ReadingArchive.unzip(ReadingArchive.java:36) at zip.ReadingArchive.unzip(ReadingArchive.java:82) at zip.ReadingArchive.unzip(ReadingArchive.java:82) at main.Main.main(Main.java:13)

究其原因是因為檔案名稱中文所致,而ZIPFile預設使用UTF-8,在遇到解壓非UTF-8的時候就會報錯
public ZipFile(File file, int mode) throws IOException {
this(file, mode, StandardCharsets.UTF_8);
}

最終我用的解決方案為
Charset gbk = Charset.forName("GBK");
ZipFile zipFile = new ZipFile(zipFileName, gbk);

當然這個肯定不適用與所有環境,可以使用的修改字元集來解決,僅供參考。

JAVA zip解壓 MALFORMED 錯誤

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.