I have decompressed base64 string of 3 document(1 .docx and 2 .pdf) and I am trying to convert these string back to their document. Here is the code I am trying. This code worked for .docx string but not for .pdf string.
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();
}
and I am getting exception on line IOUtils.copy(zi, fos);
Exception for one .pdf string is
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)
and exception for 2nd .pdf string is
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)
Please suggest me reasons for these exceptions and how can I resolve these exceptions