It's a technical note because I had the opportunity to implement a process that I don't know how much demand is in the world: getting a list of the contents of a zip file and its uncompressed size.
First of all, as a preliminary preparation, prepare the zip file according to the following procedure.
mkdir -p dir0/dir1/dir2
echo -n 1 > dir0/file1
echo -n 22 > dir0/dir1/file2
echo -n 333 > dir0/dir1/dir2/file3
cd dir0
zip -r ../zipfile.zip *
cd ..
If you run the above commands in order from the top, a zip file called zipfile.zip will be created. To verify the contents, unzip zipfile.zip according to the following procedure, and record the stored file and the list of the size before compression.
unzip -d dst zipfile.zip
cd dst
find . -type f -exec stat -c %n=%sB {} \;
# ./dir1/dir2/file3=3B
# ./dir1/file2=2B
# ./file1=1B
The preparation is now complete. The Java code for the subject is as follows.
Main.java
import java.io.IOException;
import java.util.zip.ZipFile;
public class Main {
public static void main(String[] args) {
try (var zipfile = new ZipFile("zipfile.zip")) {
var entries = zipfile.entries();
while (entries.hasMoreElements()) {
var entry = entries.nextElement();
if (entry.isDirectory()) {
continue;
}
System.out.printf("%s=%dB%n", entry.getName(), entry.getSize());
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
If you do this, you will get output similar to the following: If you compare it, you can see that it matches the contents of the preparation (`・ ω ・ ´) Shakin
dir1/dir2/file3=3B
dir1/file2=2B
file1=1B
Recommended Posts