There is a requirement that HTTP gets to the byte array of the zip file and needs to be converted to a local zip or unzip a zip file.
1. Class to use
Import Java.io.FileOutputStream;
Import Java.util.zip.ZipEntry;
Import Java.util.zip.ZipInputStream;
Import Java.util.zip.ZipOutputStream;
Import Java.io.ByteArrayInputStream;
2 Get the extracted files
private static void Gettxtfile (byte[] data) throws Exception {
Zipinputstream zipstream = new Zipinputstream (New Bytea Rrayinputstream (data));
ZipEntry entry = null;
while ((entry = Zipstream.getnextentry ())!= null) {
String entryname = Entry.getname ();
FileOutputStream out = new FileOutputStream ("/log/111/" + entryname);
byte[] Bytebuff = new byte[4096];
int bytesread = 0;
while ((Bytesread = Zipstream.read (bytebuff))!=-1) {
out.write (bytebuff, 0, bytesread);
}
Out.close ();
Zipstream.closeentry ();
}
Zipstream.close ();
}
3. Dump zip file, (can modify the file name in Zip)
private static void Getzipfile (byte[] data throws Exception {
String filename = "/log/111/111.zip";
FileOutputStream FileOutputStream = new FileOutputStream (filename);
Zipoutputstream Zos = new Zipoutputstream (fileoutputstream);
Zipinputstream ZIPstream = new Zipinputstream (new Bytearrayinputstream (data));
ZipEntry entry;
while ((entry = Zipstream.getnextentry ())!= null) {
ZipEntry entry1 = new ZipEntry (Entry.getname ());
Zos.putnextentry (entry1);
Zipstream.closeentry ();
}
Zos.write (data);
Zos.flush ();
Zos.closeentry ();
Zos.close ();
Zipstream.close ();
}
or simply brute-saving it directly to a local file.
private static void Getzipfile (byte[] data throws Exception {
String filename = "/log/111/111.zip";
File TargetFile = new file (filename);
OutputStream OutStream = new FileOutputStream (targetfile);
Outstream.write (data);
Outstream.flush ();
Outstream.close ();
}