code works and CG is nitpicking, wanting to see that one special thing they don't talk about in their descriptions.
What about here. I'm reading the zip archive. So what is the problem? Do they ask to read the zips content into memory? Do they just want me to try to validate more than 100 times? The FileTree task already drove me crazy (rally this time). It's about nio and they ask for File. Then lambdas don't work on their server, next type inference throws errors on CG box. Renaming has to be done right away otherwise you're in validation hell. After 30 tries I added a linebreak between written file contents and finally it worked. Why can't they just say 'we want to see things exactly this or that way... to the POINT'. This pisses me off big time. Sorry guys 😜🤪
Oh, I just see that CG is using the version where I commented out the try block to see if that is the problem. So don't mind the missing close calls.
package com.codegym.task.task31.task3105;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.*;
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 {
if (args[0] == null || args[1] == null)
throw new IllegalArgumentException();
Path fileName = Paths.get(args[0]);
Path zipFile = Paths.get(args[1]);
Path newPath = Paths.get("new");
// here we copy the content of the zipFile
Path tempZip = Files.createTempFile("zip", ".zip");
// try (ZipInputStream zin = new ZipInputStream(new FileInputStream(zipFile.toString()));
// ZipOutputStream zout = new ZipOutputStream(new FileOutputStream(tempZip.toString()))) {
ZipInputStream zin = new ZipInputStream(new FileInputStream(zipFile.toString()));
ZipOutputStream zout = new ZipOutputStream(new FileOutputStream(tempZip.toString()));
// get the realtive path of the filename to add inside the zip archive, as said we want to check if it exists
String toAddRealativePath = newPath.resolve(fileName.getFileName()).toString();
// loop over all zipEntries of the orig file
ZipEntry zipEntry;
while ((zipEntry = zin.getNextEntry()) != null) {
// we have the input zip entry open
// first check if it matches the name of the toAdd file, if so, skip
if (!zipEntry.getName().equals(toAddRealativePath)) {
// then create a ZipEntry for the output file, the temp file
// it has the same credentials as the in zipEntry
ZipEntry outZipEntry = new ZipEntry(zipEntry);
zout.putNextEntry(outZipEntry);
// copy, I use a byte array as buffer instead of a BufferedStream
int len;
byte[] buffer = new byte[1024 * 8];
while ((len = zin.read(buffer)) > 0) { // available did not work
zout.write(buffer, 0, len);
}
zout.closeEntry();
zin.closeEntry();
}
}
// now add the fileName
ZipEntry outZipEntry = new ZipEntry(toAddRealativePath);
zout.putNextEntry(outZipEntry);
// instead of using the copy method it probably would be more consistent to outsource the copy routine above
// and reuse this as method here
Files.copy(fileName, zout);
zout.closeEntry();
//} // end try
// if we are here, then no exception occurred and we can move the temp zip file to the orig location
Files.deleteIfExists(zipFile);
Files.move(tempZip, zipFile);
}
}