From 511e6511b2ee277d14f122bc444b40dae7be46b5 Mon Sep 17 00:00:00 2001 From: Victor Date: Mon, 11 Nov 2013 20:09:03 +0200 Subject: [PATCH] =?UTF-8?q?=D0=9B=D0=A03.=20=D0=94=D0=BE=D0=BF=D0=BE=D0=BB?= =?UTF-8?q?=D0=BD=D0=B8=D1=82=D0=B5=D0=BB=D1=8C=D0=BD=D0=BE=D0=B5=20=D0=B7?= =?UTF-8?q?=D0=B0=D0=B4=D0=B0=D0=BD=D0=B8=D0=B5.=20=D0=A0=D0=B0=D1=81?= =?UTF-8?q?=D0=BF=D0=B0=D0=BA=D0=BE=D0=B2=D0=BA=D0=B0=20beta.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/tse/lr3/DirZip.java | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/src/tse/lr3/DirZip.java b/src/tse/lr3/DirZip.java index 2f8cbed..ce45275 100644 --- a/src/tse/lr3/DirZip.java +++ b/src/tse/lr3/DirZip.java @@ -5,6 +5,7 @@ import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.zip.ZipEntry; +import java.util.zip.ZipInputStream; import java.util.zip.ZipOutputStream; /** @@ -33,8 +34,11 @@ public class DirZip extends AbstractDirectoryChooser { @Override protected void directorySelected(File directory) { try { - if (EXTRACT) { - + if (!EXTRACT) { + FileInputStream fis = new FileInputStream(zipFile); + ZipInputStream zis = new ZipInputStream(fis); + unzipDirectory(zis, directory); + zis.close(); } else { // Запаковываем FileOutputStream fos = new FileOutputStream(zipFile); @@ -47,6 +51,33 @@ public class DirZip extends AbstractDirectoryChooser { } } + private void unzipDirectory(ZipInputStream zis, File directory) throws IOException { + ZipEntry entry; + while ( (entry = zis.getNextEntry()) != null) { + if (entry.isDirectory()) { + // Создаём папку + new File(directory, entry.getName()).mkdirs(); + } else { + String filename = entry.getName(); + int separatorIndex = filename.lastIndexOf(File.separator); + if (separatorIndex != -1) { + new File(directory, filename.substring(0, separatorIndex)).mkdirs(); + filename = filename.substring(separatorIndex); + } + // Распаковываем файл + byte[] buffer = new byte[1024]; + FileOutputStream fos = new FileOutputStream(new File(directory, filename)); + int length; + while ( (length = zis.read(buffer)) > 0 ) { + fos.write(buffer, 0, length); + } + fos.flush(); + fos.close(); + } + } + zis.closeEntry(); + } + private void zipDirectory(ZipOutputStream zos, String sourceDir, File fileSource) throws IOException { File[] files = fileSource.listFiles(); for (File file : files) {