I googled different ways how to unzip a file etc, but the information i find is so different and I really don't know how to make this work.
On my computer the program seems to work, I end up getting a zip file which contains a.txt , b.txt and a new/ folder with result.mp3 .
I am so frustrated, because I can't really find a way how to do this properly, just a bunch of different ways which I don't understand. The little info provided by CG doesn't help at all.
The out-commented lines are different approaches which didn't work as well.
Another point which I don't understand - in the Conditions it states " don't use the file system" . But when I used another way of adding the file to the archive (e.g. putting another ZipEntry to the List), the validator said: "To write a file to the archive stream, use the Files.copy(Path, OutputStream) method."
Can someone help me or give me a proper source of information for this topic?
Thanks in advance.
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.List;
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 fileName = Paths.get(args[0]); // /Users/test/Desktop/Folder/result.mp3
Path zipFile = Paths.get(args[1]); // /Users/test/Desktop/Folder/test.zip
*/
String fileName = args[0];
String zipFile = args[1];
List<ZipEntry> zipFiles = new ArrayList<>();
//zipFiles.add(new ZipEntry("new/" + Paths.get(fileName).getFileName()));
ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(zipFile));
ZipEntry zipEntry = null;
while((zipEntry = zipInputStream.getNextEntry()) != null){
zipFiles.add(zipEntry);
}
zipInputStream.close();
FileOutputStream outStream = new FileOutputStream(zipFile);
ZipOutputStream zipOutputStream = new ZipOutputStream(outStream);
File file = new File(fileName);
zipOutputStream.putNextEntry(new ZipEntry("new/" + file.getName()));
Files.copy(file.toPath(), zipOutputStream);
//zipOutputStream.putNextEntry(new ZipEntry("new/" + Paths.get(fileName).getFileName()));
for(ZipEntry z : zipFiles){
zipOutputStream.putNextEntry(z);
}
zipOutputStream.close();
}
}