Jun 14
Java ile aşağıdaki kodu kullanarak sıkıştırılmış olan zip/jar dosyalarını istediğimiz klasör altına açabiliriz. Bunun için herhangi bir kütüphaneye ihtiyaç yoktur. java.util.zip.ZipEntry sınıfını kullanarak bu işlemi gerçekleştirebiliriz.
// birinci parametre zip veya jar dosyası // ikinci parametre bu sıkıştırılmış dosyanın // çıkartılacağı klasör // extractZip("C:\\jsp-api.jar", "C:\\"); public void extractZip(String zip, String destinationDir) { ZipFile zipFile = null; try { zipFile = new ZipFile(zip); Enumeration entries = zipFile.entries(); byte[] buffer = new byte[1024]; int length; while (entries.hasMoreElements()) { ZipEntry entry = (ZipEntry) entries.nextElement(); // eğer klasör bulunursa if (entry.isDirectory()) { (new File(destinationDir + entry.getName())).mkdir(); } else { InputStream in = zipFile.getInputStream(entry); FileOutputStream fos = null; fos = new FileOutputStream(destinationDir + entry.getName()); BufferedOutputStream out = new BufferedOutputStream(fos); // dosyanın içeriği byte olarak yazılıyor while ((length = in.read(buffer)) >= 0) { out.write(buffer, 0, length); } in.close(); out.close(); } } } catch (IOException exc) { exc.printStackTrace(); } finally { if(zipFile != null) { try { zipFile.close(); } catch(Exception exc) {} } } }

Son Yorumlar