To my fellow CodeGym friends,
I do not know what could possibly be wrong with my code. I have spent so long working on this, and I will admit to even cheating by looking at the solution. I fail to see any cause for my lack of validation. I hope that some of you experienced solvers out there can show me where I went wrong.
- The top 2 points pass, the bottom 3 fail.
Thanks,
Dan
package com.codegym.task.task31.task3105;
import java.io.*;
import java.nio.file.Files;
import java.util.HashMap;
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 {
HashMap<ZipEntry, byte[]> fileMap = getContents(args[1]);
FileOutputStream fileOutStream = new FileOutputStream(args[1]);
ZipOutputStream zipOutStream = new ZipOutputStream(fileOutStream);
File originalFile = new File(args[0]);
zipOutStream.putNextEntry(new ZipEntry("new/" + originalFile.getName()));
Files.copy(originalFile.toPath(), zipOutStream);
zipOutStream.closeEntry();
for (Map.Entry pair : fileMap.entrySet()) {
ZipEntry nextZip = (ZipEntry) pair.getKey();
byte[] content = (byte[]) pair.getValue();
String fileName = nextZip.getName();
if (!fileName.equals("new/" + originalFile.getName())) {
zipOutStream.putNextEntry(nextZip);
zipOutStream.write(content);
zipOutStream.closeEntry();
}
}
zipOutStream.close();
fileOutStream.close();
}
private static HashMap<ZipEntry, byte[]> getContents(String args1) throws IOException {
HashMap<ZipEntry, byte[]> resultMap = new HashMap<>();
FileInputStream inStream = new FileInputStream(args1);
ZipInputStream zipInStream = new ZipInputStream(inStream);
ZipEntry nextEntry;
// Loop where nextEntry is the next file available for reading
while ((nextEntry = zipInStream.getNextEntry()) != null) {
ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length;
// This loop puts up to 1024 bytes of info at a time into byteArray
while ((length = zipInStream.read(buffer)) > 0) {
byteArray.write(buffer, 0, length);
}
// Now byteArray contains all bytes from the nextEntry file
resultMap.put(nextEntry, byteArray.toByteArray());
}
return resultMap;
}
}