It works in local computer but fails in tests.
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.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;
/*
Adding a file to an archive
*/
public class Solution {
public static void main(String[] args) throws IOException {
// Get arguments
String fileName = args[0];
String zipFilePath = args[1];
String onlyFileName = fileName.substring(fileName.lastIndexOf(File.separator) + 1);
// Copy zip entries to a zip entry list
ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFilePath));
ZipEntry ze = null;
Map<ZipEntry, byte[]> zipMap = new HashMap<>();
byte[] buffer = null;
while ((ze = zis.getNextEntry()) != null) {
buffer = new byte[(int)ze.getSize()];
zis.read(buffer);
zipMap.put(ze,buffer);
}
// close zip input stream
zis.close();
// Open Zip Output Stream
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFilePath));
// 3. You need to write the contents of the file passed to main as the first argument to the ZipOutputStream.
zos.putNextEntry(new ZipEntry("new/" + onlyFileName));
Files.copy(Paths.get(fileName), zos);
// 4. You need to write all of the other content read from the ZipInputStream to the ZipOutputStream.
for(Map.Entry<ZipEntry, byte[]> entry : zipMap.entrySet()){
zos.putNextEntry(entry.getKey());
zos.write(entry.getValue());
}
// close zip output stream
zos.close();
}
}