0

I have a encoded base64 string from a zip file. I try to decode the string and convert to a zip file. I have this code:

Path rootDirectory = FileSystems.getDefault().getPath("/home/myName/driversFolder");
Path tempDirectory = Files.createTempDirectory(rootDirectory, "");
String dirPath = tempDirectory.toString();
String zipfile = "rbdkjsd4934234234adadds==" //base64 string test
FileOutputStream fos = new FileOutputStream(dirPath + "/zipf.zip");
fos.write(Base64.getDecoder().decode(tt));
fos.close();

If I go to the folder, I can see a zip file (zipf.zip) with the original content and apparently it's a zip .. but it's not. I'm using linux and if i try to unzip zipf.zip in command line i have the error:

End-of-central-directory signature not found. Either this file is not a zipfile, or it constitutes one disk of a multi-part archive. In the latter case the central directory and zipfile comment will be found on the last disk(s) of this archive.

Apparently it is a zip because file has a .zip name, but is not in zip format. So what is the way to solve this?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
RMRMaster
  • 89
  • 1
  • 11
  • Use ZipOutputStream instead. https://stackoverflow.com/questions/1091788/how-to-create-a-zip-file-in-java – Lukas Bradley Aug 07 '17 at 15:49
  • 2
    You're not really "converting it to a zip file" - you're saving the data into a file with an extension of `.zip` but that's all. If the data isn't already compressed, you need to be doing the compression part yourself. (e.g. via ZipOutputStream.) – Jon Skeet Aug 07 '17 at 15:50
  • Can you provide some examples? i try to use zipOutPutStream with my code but i not understand. Thanks for the replys – RMRMaster Aug 07 '17 at 15:54
  • 2
    Don't you think there might already be some examples online? – shmosel Aug 07 '17 at 15:58
  • Yes i think but i cannot understand because in the example that i see we use the zipEntry and put singles files (txt for example).. in by case i need to put the base64 decoded because the original zip file have a lot of folders and files.... – RMRMaster Aug 07 '17 at 16:02
  • What do you get if you run `file zipf.zip`? – André Onuki Aug 07 '17 at 17:12
  • @Meião the shell return: 7-zip archive data – RMRMaster Aug 08 '17 at 07:26
  • So it looks like you are putting the correct headers on the file, though the contents aren't correct. You should check ZipOutputStream as mentioned before. – André Onuki Aug 08 '17 at 14:11

0 Answers0