public static void main(String[] args) throws IOException {
try(ZipInputStream in = new ZipInputStream(new FileInputStream(args[1]));
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(args[1]))){
HashMap<String, byte[]> map = new HashMap<>();
while (in.available()!=0){
ZipEntry ent = in.getNextEntry();
byte[] tmpArr = new byte[(int)ent.getSize()];
int count = in.read(tmpArr);
byte[] nex = new byte[count];
System.arraycopy(tmpArr, 0, nex, 0, count);
map.put(ent.getName(), nex);
in.closeEntry();
}
Path adding = Paths.get(args[0]);
out.putNextEntry(new ZipEntry(Paths.get("new",adding.getFileName().toString()).toString()));
Files.copy(adding, out);
out.closeEntry();
for(Map.Entry<String, byte[]> ql: map.entrySet()){
if(!Paths.get(ql.getKey()).getFileName().toString().equals(adding.getFileName().toString())){
out.putNextEntry(new ZipEntry(ql.getKey()));
out.write(ql.getValue());
out.flush();
out.closeEntry();
}else{
out.putNextEntry(new ZipEntry(ql.getKey()));
Files.copy(adding, out);
out.closeEntry();
}
}
}
}
package com.javarush.task.task31.task3105;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
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 {
try(ZipInputStream in = new ZipInputStream(new FileInputStream(args[1]));
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(args[1]))){
HashMap<String, byte[]> map = new HashMap<>();
while (in.available()!=0){
ZipEntry ent = in.getNextEntry();
byte[] tmpArr = new byte[(int)ent.getSize()];
int count = in.read(tmpArr);
byte[] nex = new byte[count];
System.arraycopy(tmpArr, 0, nex, 0, count);
map.put(ent.getName(), nex);
in.closeEntry();
}
Path adding = Paths.get(args[0]);
out.putNextEntry(new ZipEntry(Paths.get("new",adding.getFileName().toString()).toString()));
Files.copy(adding, out);
out.closeEntry();
for(Map.Entry<String, byte[]> ql: map.entrySet()){
if(!Paths.get(ql.getKey()).getFileName().toString().equals(adding.getFileName().toString())){
out.putNextEntry(new ZipEntry(ql.getKey()));
out.write(ql.getValue());
out.flush();
out.closeEntry();
}else{
out.putNextEntry(new ZipEntry(ql.getKey()));
Files.copy(adding, out);
out.closeEntry();
}
}
}
}
}