2

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

user1432292
  • 57
  • 2
  • 9

1 Answers1

1

Your path of processing datas is then:

  • file_name_containing_decompressed_base64_string

  • => decodeBase64 => zipData (compressed or not ?)

- => GZIPInputStream : you want to decompress already-decompressed ?

  • copy to PDF

The GZIPInputStream is to be used to decompress an incoming InputStream.

1 easy solution : I would remove the decompressing, but incoherent with the name : zipdata.

2 step by step: do a program with every step: coding/decoding, compress/decompress, and check at each step, and put some println/trace

Some other posts about that: Java: How do I convert InputStream to GZIPInputStream?

Community
  • 1
  • 1