0

I am trying to save a base64 decoded string into a zip file using the mentioned code:

Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("/home/wemohamm/Desktop/test.zip")));
out.write(decodedString);
out.close();

Here decodedString contains base64 decoded string and I can output it. I am running the code in rhel6 with Java 1.6. When I try to open the zip, it says error occurred while opening file.

The same code if I use with Windows 7 Java 1.6 with path c:\\test\test.zip is working fine.

Is the zip not getting saved correctly in rhel6 or if there is any code modifications I need to do?

Qiu
  • 5,651
  • 10
  • 49
  • 56
Nadeem
  • 77
  • 1
  • 12
  • why dont you create a correct zip file with java using ZipEntry? – Tator Jun 16 '15 at 13:12
  • The object decodedString is actually a base64 decoded string. And I have no idea how to save it as zip. By the way above works fine in windows.If something is wrong or to be modified, Please share it – Nadeem Jun 16 '15 at 13:15
  • http://stackoverflow.com/questions/4773778/creating-zip-archive-in-java – Andrew Jun 16 '15 at 13:17
  • you dont create a zip archive but a file and name it .zip use @Andrew s link. – Tator Jun 16 '15 at 13:17
  • Actually here decodedSting is a zip file stream. Why zip an already zipped stream. – Nadeem Jun 16 '15 at 13:21
  • to create a correct zip archive – Tator Jun 16 '15 at 13:21
  • A "Base64 decoded string", what is that exactly? Can you show how you do the decoding? Note that a `String` is not suitable for storing arbitrary bytes. Store bytes in a `byte[]` and not in a `String`. – Jesper Jun 16 '15 at 13:23
  • I have a webservice sending a zip file in base64 encoded format in one of the xml tags. I am extracting it from the tags decoding it and then storing it in string named decodedString. – Nadeem Jun 16 '15 at 13:28
  • You are confusing us with `decodedString`. Can you please show more clearly what it is? Is it really a string? Or is it actually a byte array that is actually nothing more than a zip file in disguise? – sstan Jun 16 '15 at 13:31
  • Pardon me for confusing ..Yes its a byte arrary.NodeList filee = element.getElementsByTagName("dp:file"); line = (Element) filee.item(0); System.out.println("file: " + getCharacterDataFromElement(line)); String fileContent=getCharacterDataFromElement(line); byte[] byteArray = Base64.decodeBase64(fileContent.getBytes()) ; String decodedString = new String(byteArray); – Nadeem Jun 16 '15 at 13:43

2 Answers2

1

Don't create a string from your byte array (String decodedString = new String(byteArray);), to then use an OutputStreamWriter to write the string, because then you are running the risk of introducing encoding issues that are platform dependent.

Just use FileOutputStream to write out the byte array (byte[] byteArray) directly to file.

Something like:

try (BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("/home/wemohamm/Desktop/test.zip"), 4096)) {
    out.write(byteArray);
}

Actually, the above requires java 1.7+, because of the new try-with-resources statement.

For Java 1.6, you can do this:

BufferedOutputStream out = null;
try {
    out = new BufferedOutputStream(new FileOutputStream("/home/wemohamm/Desktop/test.zip"), 4096);
    out.write(byteArray);
} finally {
    if (out != null) {
        out.close();
    }
}
sstan
  • 35,425
  • 6
  • 48
  • 66
  • I am using jre 1.6, eclipse points out error and suggests changing to jre1.7 which I cant do, since this code is to run on jre 1.6 – Nadeem Jun 16 '15 at 14:01
0

That won't work. You are writing to a ordinary file without packing the content. Use the java zip library with ZipOutputStream, ZipEntry and so on.

chris
  • 1,685
  • 3
  • 18
  • 28