ЛР3. Дополнительное задание.

This commit is contained in:
Victor 2013-11-11 21:48:18 +02:00
parent 511e6511b2
commit f71865ae38
2 changed files with 8 additions and 8 deletions

View File

@ -15,7 +15,7 @@ public class LR_3 implements ILabRab {
"(Де)Сериализация коллекций", "(Де)Сериализация коллекций",
"Копирование 3-дневных файлов", "Копирование 3-дневных файлов",
"Поиск текста в файлах", "Поиск текста в файлах",
"Архивирование" "Работа с ZIP-архивами"
}; };
@Override @Override

View File

@ -34,7 +34,7 @@ public class DirZip extends AbstractDirectoryChooser {
@Override @Override
protected void directorySelected(File directory) { protected void directorySelected(File directory) {
try { try {
if (!EXTRACT) { if (EXTRACT) {
FileInputStream fis = new FileInputStream(zipFile); FileInputStream fis = new FileInputStream(zipFile);
ZipInputStream zis = new ZipInputStream(fis); ZipInputStream zis = new ZipInputStream(fis);
unzipDirectory(zis, directory); unzipDirectory(zis, directory);
@ -54,19 +54,19 @@ public class DirZip extends AbstractDirectoryChooser {
private void unzipDirectory(ZipInputStream zis, File directory) throws IOException { private void unzipDirectory(ZipInputStream zis, File directory) throws IOException {
ZipEntry entry; ZipEntry entry;
while ( (entry = zis.getNextEntry()) != null) { while ( (entry = zis.getNextEntry()) != null) {
final String name = entry.getName();
if (entry.isDirectory()) { if (entry.isDirectory()) {
// Создаём папку // Создаём папку
new File(directory, entry.getName()).mkdirs(); new File(directory, name).mkdirs();
} else { } else {
String filename = entry.getName(); int separatorIndex = name.lastIndexOf(File.separator);
int separatorIndex = filename.lastIndexOf(File.separator);
if (separatorIndex != -1) { if (separatorIndex != -1) {
new File(directory, filename.substring(0, separatorIndex)).mkdirs(); // Создаём папки на пути к файлу
filename = filename.substring(separatorIndex); new File(directory, name.substring(0, separatorIndex)).mkdirs();
} }
// Распаковываем файл // Распаковываем файл
byte[] buffer = new byte[1024]; byte[] buffer = new byte[1024];
FileOutputStream fos = new FileOutputStream(new File(directory, filename)); FileOutputStream fos = new FileOutputStream(new File(directory, name));
int length; int length;
while ( (length = zis.read(buffer)) > 0 ) { while ( (length = zis.read(buffer)) > 0 ) {
fos.write(buffer, 0, length); fos.write(buffer, 0, length);