It's my second approach - still all I can see is red light!
Any sugesstions ?
package com.codegym.task.task31.task3105;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.*;
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 {
//My new strategy:
//1 - create Map with file content as byte[] and file-name as String
//2 - put to Map file-to-add-to-archive with it's name prefixed with "new\"
//3 - Unzip all files from archive to Map
//4 - Iterate through map and write bytes to entries
Map<byte[],String> map = new HashMap<>();
//1 is done!
//2 - put to Map file-to-add-to-archive with it's name prefixed with "new\"
File fileToAddToArchive = new File(args[0]); //file to add to archive
FileInputStream fileInputStream = new FileInputStream(fileToAddToArchive);
while (fileInputStream.available()>0){
byte[] byteArray = new byte[124];
fileInputStream.read(byteArray);
map.put(byteArray, "new\\" + fileToAddToArchive.getName());
}
fileInputStream.close();
//2 is done!
//printMap(map);
//3 - Unzip all files from archive to Map
File zipFile = new File(args[1]); //archive
ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(zipFile));
ZipEntry a;
while ((a = zipInputStream.getNextEntry())!=null){
if (a.getName().equals("new\\" + Paths.get(args[0]).getFileName())) continue;
if (!a.isDirectory()) { //if this is not a directory (this function is not working properly)
while (zipInputStream.available() > 0) {
byte[] byteArray = new byte[124];
zipInputStream.read(byteArray);
map.put(byteArray,a.getName());
zipInputStream.closeEntry();
}
}
}
zipInputStream.closeEntry();
zipInputStream.close();
//3 is done!
//printMap(map);
//4 - Iterate through map and write bytes to entries
ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(args[1]));
for (Map.Entry<byte[],String> para : map.entrySet()){
zipOutputStream.putNextEntry(new ZipEntry(para.getValue()));
zipOutputStream.write(para.getKey());
}
zipOutputStream.flush();
zipOutputStream.close();
//4 is done!
}
}