Что не так реализовано?
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;
/*
Добавление файла в архив
*/
public class Solution {
public static void main(String[] args) throws IOException {
Map<ZipEntry, Path> entries = new HashMap<>();
//Uncompress zip and put file to hashmap
try (ZipInputStream zis = new ZipInputStream(new FileInputStream(args[1]))) {
ZipEntry entry;
while ((entry = zis.getNextEntry()) != null) {
Path file = Files.createTempFile("zip", ".tmp");
FileOutputStream fos = new FileOutputStream(file.toFile());
byte[] buff = new byte[1024];
int len;
while((len = zis.read(buff)) > 0) {
fos.write(buff, 0 ,len);
}
entries.put(entry, file);
fos.close();
}
}
//Check exist file in entries
Path sourceFile = Paths.get(args[0]);
entries.forEach((k, v) -> {
if (k.getName().equals(sourceFile.getFileName().toString())) {
entries.put(k, sourceFile);
}
});
if (!entries.containsValue(sourceFile)) {
entries.put(new ZipEntry("new/" + sourceFile.getFileName().toString()), sourceFile);
}
//Write to zip
try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(args[1]))) {
entries.forEach((k, v) -> {
try (FileInputStream fis = new FileInputStream(v.toFile())){
zos.putNextEntry(new ZipEntry(k.getName()));
byte[] buff = new byte[1024];
int len;
while((len = fis.read(buff)) >= 0) {
zos.write(buff, 0 ,len);
}
} catch (IOException e) {
e.printStackTrace();
}
});
}
}
}