прописал последнюю строку зависать перестала в тесте, но куча ошибок.
do {
k = zS.read(b1, 0, 1);
if (k > 0) {
str.append(new String(b1));
}
j++;
} while (k > 0 && j < 1000);
package com.javarush.task.task31.task3105;
import java.io.File;
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.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
/*
Добавление файла в архив
*/
public class Solution {
// прочитьаь все
private static String fx(ZipInputStream zS) throws IOException {
StringBuilder str = new StringBuilder("");
int k;
byte[] b1 = new byte[1];
int j=0;
do {
k = zS.read(b1, 0, 1);
if (k > 0) {
str.append(new String(b1));
}
j++;
} while (k > 0 && j < 1000);
return str.toString();
}
public static void main(String[] args) throws IOException {
FileInputStream fO = new FileInputStream(args[1]);
ZipInputStream zS = new ZipInputStream(fO);
HashMap<String, byte[]> hm = new HashMap<>();
ZipEntry ze = null;
while ((ze = zS.getNextEntry()) != null) {
if (ze != null) {
hm.put(ze.getName(), fx(zS).getBytes());
}
}
zS.close();
fO.close();
// ---------------------------
FileOutputStream fOS = new FileOutputStream(args[1]);
ZipOutputStream zOS = new ZipOutputStream(fOS);
File f = new File(args[0]);
for (String k : hm.keySet()) {
File arName = new File(k);
if (!arName.getPath().endsWith("new\\" + f.getName())) {
zOS.putNextEntry(new ZipEntry(k));
zOS.write(hm.get(k), 0, hm.get(k).length);
zOS.flush();
zOS.closeEntry();
}
}
zOS.putNextEntry(new ZipEntry("new/" + f.getName()));
Files.copy(Paths.get(args[0]), zOS);
zOS.closeEntry();
zOS.close();
fOS.close();
}
}