Hello guys,
Hope that you're fine. My code works fine locally but I can't pass the requirements four. I need help to debug it.
Thanks.
package com.codegym.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;
/*
Adding a file to an archive
*/
public class Solution {
public static void main(String[] args) throws IOException {
Path zipFile = Paths.get(args[1]);
try(ZipInputStream zis = new ZipInputStream(Files.newInputStream(zipFile.toAbsolutePath()))){
Map<ZipEntry, byte[]> storage = new HashMap<>();
ZipEntry zipEntry;
ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
while ((zipEntry = zis.getNextEntry()) != null) {
int len = 0;
byte[] buffer = new byte[1024];
while ((len = zis.read(buffer)) > 0) {
byteArray.write(buffer, 0, len);
}
storage.put(zipEntry, byteArray.toByteArray());
byteArray.reset();
}
byteArray.close();
try (ZipOutputStream zos = new ZipOutputStream(Files.newOutputStream(zipFile))) {
Path fileToZip = Paths.get(args[0]);
zos.putNextEntry(new ZipEntry("new/" + fileToZip.getFileName()));
Files.copy(fileToZip, zos);
for (Map.Entry<ZipEntry, byte[]> entry : storage.entrySet()) {
zos.putNextEntry(entry.getKey());
zos.write(entry.getValue());
}
} catch (IOException e){
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}