0

I have the contents of a zip file that I received from a clients multipart form data api stored as a String.

I simply want to save this data now as a zip file; however, when I try saving to a file as below then when I attempt to open the file I get a message stating

"Windows cannot open the folder. The compressed (zipped) Folder 'C:\payload.zip' is invalid."

public void createFile(String data) {
    try {
        BufferedWriter out = new BufferedWriter(new FileWriter("c:\\payload.zip"));
        out.write(data);   
        out.close();
    }
    catch (IOException e)
    {
        System.out.println("Exception ");

    }
}

I am simply passing the String that I receive to the little test createFile method shown above.

I thought that I would paste the actual String contents below but when I attempt to do so it converts it to this (Without the double quotes): " PK"

Any help with what I am doing wrong?

Malloc
  • 55
  • 8
  • 1
    A zipfile is a binary format not just plain text. Use `ZipOutputStream` to write your data – Reimeus Aug 08 '17 at 16:38
  • The moment you start treating binary data as a `String`, you're doomed. In your case this happens when receiving the form data. – Kayaman Aug 08 '17 at 16:44

2 Answers2

1

You can create a file with .zip extension using BufferedWriter but don't expect that file to be a compression file (which is binary)

You can use something like below

Look at this example:

StringBuilder sb = new StringBuilder();
sb.append("your string data");

File f = new File("c:\\payload.zip");
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(f));
ZipEntry e = new ZipEntry("myFile.txt");
out.putNextEntry(e);
byte[] data = sb.toString().getBytes();
out.write(data, 0, data.length);
out.closeEntry();
out.close();
Jeeppp
  • 1,553
  • 3
  • 17
  • 39
0

To save the text representation of your zip as a zip file again:

BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
                                               new FileOutputStream("c:\\dest.zip"),
                                               "Cp1252"));
writer.write(data);   
writer.close();

Or you can try:

FileOutputStream fos = new FileOutputStream("C:\\dest.zip");
fos.write(data.getBytes());
fos.close();
Vanna
  • 746
  • 1
  • 7
  • 16
  • Hi Vanna, yes you are correct, I do already have the zip file contents and simply need to write them to a zip file on my local file system. I tried your example above with the Cp1252 encoding; however, I received the same error message when attempting to open the file "Windows cannot open the folder. The Compressed (zipped) Folder 'C\dest.zip' is invalid." any thoughts? – Malloc Aug 08 '17 at 17:17
  • Thank you, unfortunately I still get the same error when attempting to extract the resulting zip file. Below are the actual contents of the zip file that I created (enclosed in double quotes) Just in case that may help. " PK ÁIKšŽÌ*• * > 511257719_511257718_837P5010X12BATCH_99000061.199786.1.999.date?»Â0E…ùNvB^lQJT1¥CéÀ§äÛkR)*`O¾Ç:–s‰ Â¥×Ï´m˜_Ï4æ!æ±G!P+ËGÄŽ 511257719_511257718_837P5010X12BATCH_99000061.199786.1.999.datPK l ñ " – Malloc Aug 08 '17 at 18:03
  • Thank you Vanna. I can see that your answer would have worked but another poster pointed out that since I have binary data that has been turned into a String it appears that the data is corrupt. I have rewritten the code completely and have a related issue. Posted a new question regarding that here: https://stackoverflow.com/questions/45578097/how-to-obtain-just-a-single-form-field-from-an-httpresponse-in-java-and-write-it – Malloc Aug 08 '17 at 21:19