Help modify the code. It unpacks Zip perfectly, but I need it to unpack files into a folder with the same name as the archive name.
private boolean unpackZip(String path, String zipname) { InputStream is; ZipInputStream zis; try { is = new FileInputStream(path + zipname); zis = new ZipInputStream(new BufferedInputStream(is)); ZipEntry ze; while((ze = zis.getNextEntry()) != null) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int count; String filename = ze.getName(); FileOutputStream fout = new FileOutputStream(path + filename); // reading and writing while((count = zis.read(buffer)) != -1) { baos.write(buffer, 0, count); byte[] bytes = baos.toByteArray(); fout.write(bytes); baos.reset(); } fout.close(); zis.closeEntry(); } zis.close(); } catch(IOException e) { e.printStackTrace(); return false; } return true; }
filename2. Check if the corresponding folderpath+filenameexists Usejava.nio.file.FilesandFiles.exists(path)3. If it does not exist, create the foldernew File("C:\\Directory1").mkdir();fromjava.io.File4. Go to the folder you created. Or add its name to the path variable. 5. Unpack. PROFIT. - gecube