Here I give you a couple of java functions that can decompress a ZIP file via code. I hope to serve you ^ _ ^
private static void unzip (File f) throws IOException {ZipFile zip
;
zip = new ZipFile (f);
zip.entries Enumeration e = ();
while (e . hasMoreElements ()) {ZipEntry zen
= (ZipEntry) e.nextElement ();
if (zen.isDirectory ()) {
continue;}
int size = (int) zen.getSize ();
InputStream zis = zip.getInputStream (zen)
extractfile = f.getParentFile String (). getAbsolutePath () + File.separator + Zen.getName ();
WriteFile (zis, new File (extractfile), size);
zis.close ();}
zip.close ();}
/ ***
* Allows Writing a jar file to disk * @ throws IOException
* / private static void WriteFile
(InputStream zis, File file, int size) throws IOException {File
file.getParentFile parentFile = ();
if (! parentFile.exists ()) {
parentFile.mkdirs ();// this is important}
FileOutputStream fos = null;
try {fos = new FileOutputStream (file);
byte [] bytestream = new byte [(int) size];
int buf = -1;
int rb = 0;
while ((((int) size - rb)> 0)) {buf = zis.read
(bytestream, rb, (int) size - rb )
if (buf == -1) {
break;}
rb + = buf;}
fos.write (bytestream);
} catch (IOException e) {throw new IOException
("UNZIP_ERROR ");
} finally {if (fos! = null) {
fos.close ();
}}}
Note: This same code works to unpack JAR's the only thing that would have to change is the "ZipFile" by "Jarfile."
0 comments:
Post a Comment