我解壓縮了3個文檔(1.docx和2 .pdf)的base64字符串,我試圖將這些字符串轉換回它們的文檔。這是我正在嘗試的代碼。此代碼適用於.docx字符串,但不適用於.pdf字符串。java.util.zip.ZipException:無效的文字/長度代碼
public static void main(String[] args) throws IOException, DataFormatException {
String outputFilePath = "document.pdf";
File file = new File(outputFilePath);
FileOutputStream fos = new FileOutputStream(file);
String str = FileUtils.readFileToString(new File(file_name_containing_compressed_base64_string), "utf-8");
byte[] zipData = Base64.decodeBase64(str);
GZIPInputStream zi = new GZIPInputStream(new ByteArrayInputStream(zipData));
IOUtils.copy(zi, fos);
fos.close();
zi.close();
}
,我在網上收到例外IOUtils.copy(zi, fos);
異常的一個.PDF字符串
java.util.zip.ZipException: invalid literal/length code
at java.util.zip.InflaterInputStream.read(Unknown Source)
at java.util.zip.GZIPInputStream.read(Unknown Source)
at java.io.FilterInputStream.read(Unknown Source)
at org.apache.commons.io.IOUtils.copyLarge(IOUtils.java:1792)
at org.apache.commons.io.IOUtils.copyLarge(IOUtils.java:1769)
at org.apache.commons.io.IOUtils.copy(IOUtils.java:1744)
at com.fileHandling.FileOutput.main(FileOutput.java:126)
和第二.PDF字符串的例外是
java.util.zip.ZipException: invalid distance too far back
at java.util.zip.InflaterInputStream.read(Unknown Source)
at java.util.zip.GZIPInputStream.read(Unknown Source)
at java.io.FilterInputStream.read(Unknown Source)
at org.apache.commons.io.IOUtils.copyLarge(IOUtils.java:1792)
at org.apache.commons.io.IOUtils.copyLarge(IOUtils.java:1769)
at org.apache.commons.io.IOUtils.copy(IOUtils.java:1744)
at com.fileHandling.FileOutput.main(FileOutput.java:126)
請給我建議這些原因例外情況以及如何解決這些例外情況
zipData被壓縮。因此,'GZIPInputStream'用於解壓縮 – user1432292
然後名稱是壞的:file_name_containing_decompressed_base64_string –
對不起......它是我的錯誤....我已經更正了問題 – user1432292