So I've reset this task so many times, trying different things, done research quite a bit, but I seem to have a mental block with this try-with-resources concept!
Finally, I found this https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html which helps but for some reason the final requirement doesn't verify. What have I left out or put in that's wrong? :O
package com.codegym.task.task21.task2110;
import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
/*
Refactoring methods
*/
public class Solution {
public static void writeZipEntriesToFile(String zipFileName, String outputFileName) {
java.nio.charset.Charset charset =
java.nio.charset.StandardCharsets.US_ASCII;
java.nio.file.Path outputFilePath =
java.nio.file.Paths.get(outputFileName);
try (java.util.zip.ZipFile zf = new java.util.zip.ZipFile(zipFileName);
java.io.BufferedWriter writer = java.nio.file.Files.newBufferedWriter(outputFilePath, charset)
) {
// Enumerate each entry
for (java.util.Enumeration entries =
zf.entries(); entries.hasMoreElements();) {
// Get the entry name and write it to the output file
String newLine = System.getProperty("line.separator");
String zipEntryName =
((java.util.zip.ZipEntry)entries.nextElement()).getName() +
newLine;
writer.write(zipEntryName, 0, zipEntryName.length());
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
}
}