0

I have a compressed base64 string of a Microsoft Word file. How to covert this compressed base64 string into its original file in java.

I have tried this code, but could not succeed. Here is the code I am trying

public static void main(String[] args) throws IOException, DataFormatException {

    File file = new File(outputFileName);

    byte[] zipData = Base64.decodeBase64(compressed_base64_string);
    GZIPInputStream zi = new GZIPInputStream(new ByteArrayInputStream(zipData));
    String result = IOUtils.toString(zi);
    zi.close();


    InputStream filedata = new ByteArrayInputStream(result.getBytes("UTF-8"));
    byte[] buff = new byte[1024];

    FileOutputStream fos = new FileOutputStream(file);
    while (filedata.read(buff) > 0) {
          fos.write(buff);
    }
    fos.close();
}   

This code is generating a zip file in which there are some xml files. But compressed_base64_string is generated from a microsoft word document. I am not able to get original document from this code. Please tell me what should I do next to get the original document

user1432292
  • 57
  • 2
  • 9
  • 1
    Zip <> GZip beware, also note that `IOUtils` provides a copy method –  Dec 24 '15 at 12:04
  • 3
    Possible duplicate of [Decode Base64 data in Java](http://stackoverflow.com/questions/469695/decode-base64-data-in-java) – malaguna Dec 24 '15 at 12:06
  • Your question is quite unclear. How is the document compressed in the first place? Why are you transforming the InputStream to a String, and then the String to a byte array? That is most probably incorrect. Just read from the GZIPInputStream directly instead of reading from the ByteArrayInputStream. – JB Nizet Dec 24 '15 at 12:18
  • I tried to use GZIPInputStream directly instead of reading from the ByteArrayInputStream, but this generates a zip file that is not possible to open – user1432292 Dec 24 '15 at 13:19
  • @RC your suggestion to use copy method of IOUtils worked – user1432292 Dec 24 '15 at 14:05
  • Can you post your working sokution as an answer and accept it? (maybe it will have some use for a futur reader) –  Dec 24 '15 at 14:16

2 Answers2

1

The following code worked for me

public static void main(String[] args) throws IOException, DataFormatException {
    String outputFilePath = "document.docx";
    File file = new File(outputFilePath);
    FileOutputStream fos = new FileOutputStream(file);
    byte[] zipData = Base64.decodeBase64(compressed_base64_string);
    GZIPInputStream zi = new GZIPInputStream(new ByteArrayInputStream(zipData));
    IOUtils.copy(zi, fos);

    fos.close();
    zi.close();

}
user1432292
  • 57
  • 2
  • 9
0
public static boolean decode(String filename) {
    try {
        byte[] decodedBytes = Base64.decode(loadFileAsBytesArray(filename), Base64.DEFAULT);
        writeByteArraysToFile(filename, decodedBytes);
        return true;
    } catch (Exception ex) {
        return false;
    }
}

public static boolean encode(String filename) {
    try {
        byte[] encodedBytes = Base64.encode(loadFileAsBytesArray(filename), Base64.DEFAULT);
        writeByteArraysToFile(filename, encodedBytes);
        return true;
    } catch (Exception ex) {
        return false;
    }
}

public static void writeByteArraysToFile(String fileName, byte[] content) throws IOException {
    File file = new File(fileName);
    BufferedOutputStream writer = new BufferedOutputStream(new FileOutputStream(file));
    writer.write(content);
    writer.flush();
    writer.close();
}

public static byte[] loadFileAsBytesArray(String fileName) throws Exception {
    File file = new File(fileName);
    int length = (int) file.length();
    BufferedInputStream reader = new BufferedInputStream(new FileInputStream(file));
    byte[] bytes = new byte[length];
    reader.read(bytes, 0, length);
    reader.close();
    return bytes;
}

encode("path/to/file");
decode("path/to/file");

Cvetomir
  • 29
  • 4