user405398
user405398

Reputation:

How to convert a InputStream to ZIP format?

I am having a InputStream Object which is actually a zip file. I want to change it back to zip file and save it. I am using DWR's FileTransfer class object to receive the uploaded data from client.

FileTransfer have 3 methods, getInputStream() is one of them. It returns InputStream from FileTransfer object.

In my case, fileTransfer object holds zip file and as well as InputStream object too. I have done, lot of searches in google. But i am not able to find one example, that illustrates InputStream to zip conversion.

Update

String zipName = file.getName();
String zipType = file.getMimeType();
InputStream zipStream = file.getInputStream();
ZipInputStream zis = new ZipInputStream(zipStream);
System.out.println("File Name: "+zipName+"\n"+"File Type: "+zipType);
int c;
File f2 = new File(DATA_STORE_LOC+dat+".zip");
path.setPath2(DATA_STORE_LOC+dat+".zip");
FileOutputStream fos = new FileOutputStream(f2);
ZipOutputStream zos = new ZipOutputStream(fos);
c = zis.read();
System.out.println(c);
while ((c = zis.read(BUFFER)) != -1) {
zos.write(BUFFER, 0, c);
}
zos.close();
zis.close();

I tried this code, by thought of a typical file copy program. I know it is false, just tried. It gives me java.util.zip.ZipException: ZIP file must have at least one entry.

Any suggestion would be really appreciative!!!!!

Upvotes: 4

Views: 35774

Answers (2)

extraneon
extraneon

Reputation: 23950

See the examples java2s, input and output. If you have more questions feel free to ask them :)

For clarity, in this input example you should do something like:

// FileInputStream fin = new FileInputStream(args[i]);
ZipInputStream zin = new ZipInputStream(ft.getInputStream());

As Don Roby correctly said, if you just want to copy you need not know the file structure and you could use for example static IOUtils.copy(in, out) to copy the file.

Further, if you do wish to extract the ZIP file contents, you should not plainly copy bytes. The ZIP file has a structure, and you extract Entries from the ZIP file, and not just bytes (see the example). Every Entry is a (compressed) file (or the data thereof) with the original name:

ZipEntry ze = null;
while ((ze = zin.getNextEntry()) != null) {
  System.out.println("Unzipping " + ze.getName());
  FileOutputStream fout = new FileOutputStream(ze.getName());
  for (int c = zin.read(); c != -1; c = zin.read()) {
  ...

Please note the javadoc of getNextEntry():

Reads the next ZIP file entry and positions the stream at the beginning of the entry data.

This positioning is crucial to get to the zipped file contents, and not the metadata.

And I do believe that you accidentally remove the first int:

c = zis.read(); // removing the first
while ((c = zis.read(BUFFER)) != -1) { // so you start with the second?

I believe you mix 2 idioms:

c = zis.read();
while(c != -1) {
   ...
   c = zis.read();
}

and:

int c;
while ((c = zis.read(BUFFER)) != -1) { // so you start with the second?
  ...
}

I think you can see the difference :)

Upvotes: 4

Don Roby
Don Roby

Reputation: 41127

If your input is a an InputStream from a zip file and your desired output is still a zip file with the same contents, you're just doing a file copy operation and shouldn't have to worry about zip at all. You just need to read from the InputStream and write to a FileOutputStream, more or less as you're doing, but without worrying about wrapping either stream in a zip-aware stream.

ZipInputStream is useful if you have to extract the contents of the zip file as separate files, i.e., to programmatically unzip. And on the other side, ZipOutputStream is used if your have the contents and need to combine them into a zip file.

Upvotes: 5

Related Questions