До запуска программы
После (внутри new лежит firstFile.txt)
Папка new
package com.javarush.task.task31.task3105;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
/*
Добавление файла в архив
/home/roman/Development/Java/Idea/TestProject/FolderForTest.zip
*/
/// ПРАВИЛЬНОЕ РЕШЕНИЕ ///
public class Solution {
public static void main(String[] args) throws IOException {
Path fileToAdd = Paths.get(args[0]);
Path zipFile = Paths.get(args[1]);
// Создаем поток чтения zip и поток записи zip
Map<ZipEntry, ByteArrayOutputStream> content = new HashMap<>();
try(ZipInputStream zipIn = new ZipInputStream(new FileInputStream(args[1]))) {
ZipEntry entry;
while ((entry = zipIn.getNextEntry()) != null){
ByteArrayOutputStream by = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int count = 0;
while ((count = zipIn.read(buf)) != -1){
by.write(buf,0,count);
}
content.put(entry, by);
zipIn.closeEntry();
}
}
try (ZipOutputStream zos = new ZipOutputStream(Files.newOutputStream(zipFile))){
for (Map.Entry<ZipEntry, ByteArrayOutputStream> pair : content.entrySet()) {
// System.out.println(Paths.get(String.valueOf(pair.getKey())).toFile().getName());
// System.out.println(fileToAdd.getFileName());
if (!(Paths.get(String.valueOf(pair.getKey())).getFileName().equals(fileToAdd.getFileName()))) {
//System.out.println(pair.getKey());
zos.putNextEntry(pair.getKey());
zos.write(pair.getValue().toByteArray());
zos.flush();
zos.closeEntry();
}
}
//System.out.println(zipFile.getFileName().toString().substring(0, zipFile.getFileName().toString().length() - 4) + "/new/" + fileToAdd.getFileName());
ZipEntry entry = new ZipEntry("new/" + fileToAdd.getFileName());
zos.putNextEntry(entry);
Files.copy(fileToAdd, zos);
zos.closeEntry();
}
}
}
// public class Solution {
// public static void main(String[] args) {
// Path fileToAdd = Paths.get(args[0]);
// Path zipFile = Paths.get(args[1]);
// ZipInputStream zipIn;
// ZipOutputStream zos;
// // Создаем поток чтения zip и поток записи zip
// Map<ZipEntry, ByteArrayOutputStream> content = new HashMap<>();
// try {
// zipIn = new ZipInputStream(Files.newInputStream(zipFile));
// ZipEntry entry;
// while ((entry = zipIn.getNextEntry()) != null){
// ByteArrayOutputStream by = new ByteArrayOutputStream();
// byte[] buf = new byte[1024];
// int count = 0;
// while ((count = zipIn.read(buf)) != -1){
// by.write(buf,0,count);
// }
// content.put(entry, by);
// zipIn.closeEntry();
// }
// zipIn.close();
// }
// catch (IOException e) {
// e.printStackTrace();
// }
// try {
// zos = new ZipOutputStream(Files.newOutputStream(zipFile));
// for (Map.Entry<ZipEntry, ByteArrayOutputStream> pair : content.entrySet()) {
// // System.out.println(Paths.get(String.valueOf(pair.getKey())).toFile().getName());
// // System.out.println(fileToAdd.getFileName());
// if (!(Paths.get(String.valueOf(pair.getKey())).getFileName().equals(fileToAdd.getFileName()))) {
// //System.out.println(pair.getKey());
// zos.putNextEntry(pair.getKey());
// zos.write(pair.getValue().toByteArray());
// zos.flush();
// zos.closeEntry();
// }
// }
// //System.out.println(zipFile.getFileName().toString().substring(0, zipFile.getFileName().toString().length() - 4) + "/new/" + fileToAdd.getFileName());
// ZipEntry entry = new ZipEntry(zipFile.getFileName().toString().substring(0, zipFile.getFileName().toString().length() - 4) + "/new/" + fileToAdd.getFileName());
// zos.putNextEntry(entry);
// Files.copy(fileToAdd, zos);
// zos.closeEntry();
// zos.close();
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
// }