public class Solution {
public static void main(String[] args) throws IOException {
Map<String, ByteArrayOutputStream> map=new HashMap<>();
ZipEntry ze;
try (ZipInputStream zIn=new ZipInputStream(new FileInputStream(args[1]))) {
while ((ze = zIn.getNextEntry()) != null) {
ByteArrayOutputStream by = new ByteArrayOutputStream();
int buffSize=1024;
byte [] buff=new byte[buffSize];
int readBytes;
while ((readBytes=zIn.read(buff, 0 , buffSize)) >-1) {
by.write(buff, 0, readBytes);
}
map.put(ze.toString(), by);
zIn.closeEntry();
}
}
String fileName = args[0].substring(args[0].lastIndexOf('/')+1);
ZipEntry newZe=new ZipEntry("new/" + fileName);
try (ZipOutputStream zOut= new ZipOutputStream(new FileOutputStream(args[1]))) {
zOut.putNextEntry(newZe);
Files.copy(Paths.get(args[0]), zOut);
zOut.closeEntry();
for (Map.Entry<String, ByteArrayOutputStream> pair: map.entrySet()) {
newZe = new ZipEntry(pair.getKey());
if(!newZe.toString().endsWith(args[0])) {
zOut.putNextEntry(newZe);
zOut.write(pair.getValue().toByteArray());
zOut.closeEntry();
}
}
}
}
}
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.ArrayList;
import java.util.HashMap;
import java.util.List;
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<String, ByteArrayOutputStream> map=new HashMap<>();
ZipEntry ze;
try (ZipInputStream zIn=new ZipInputStream(new FileInputStream(args[1]))) {
while ((ze = zIn.getNextEntry()) != null) {
ByteArrayOutputStream by = new ByteArrayOutputStream();
int buffSize=1024;
byte [] buff=new byte[buffSize];
int readBytes;
while ((readBytes=zIn.read(buff, 0 , buffSize)) >-1) {
by.write(buff, 0, readBytes);
}
map.put(ze.toString(), by);
zIn.closeEntry();
}
}
String fileName = args[0].substring(args[0].lastIndexOf('/')+1);
ZipEntry newZe=new ZipEntry("new/" + fileName);
try (ZipOutputStream zOut= new ZipOutputStream(new FileOutputStream(args[1]))) {
zOut.putNextEntry(newZe);
Files.copy(Paths.get(args[0]), zOut);
zOut.closeEntry();
for (Map.Entry<String, ByteArrayOutputStream> pair: map.entrySet()) {
newZe = new ZipEntry(pair.getKey());
if(!newZe.toString().endsWith(args[0])) {
zOut.putNextEntry(newZe);
zOut.write(pair.getValue().toByteArray());
zOut.closeEntry();
}
}
}
}
}