I want to get a list of the contents of a zip file and its uncompressed size

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

I want to get a list of the contents of a zip file and its uncompressed size
I want to recursively get the superclass and interface of a certain class
I want to get a list of only unique character strings by excluding fixed character strings from the file name
I want to find the MD5 checksum of a file in Java and get the result as a string in hexadecimal notation.
I want to var_dump the contents of the intent
I managed to get a blank when I brought the contents of Beans to the textarea
I want to be aware of the contents of variables!
I want to make a list with kotlin and java!
I want to call a method and count the number
I made a tool to output the difference of CSV file
[Spring Boot] I want to add my own property file and get the value with env.getProperty ().
The story of forgetting to close a file in Java and failing
[Ruby] I want to extract only the value of the hash and only the key
If you want to make a zip file with Ruby, it's rubyzip.
I want to get the field name of the [Java] field. (Old tale tone)
I want to download a file on the Internet using Ruby and save it locally (with caution)
I want to output the day of the week
I want to get the value in Ruby
Output the sum of each name and its contents from a multiple array
I want to get the value of Cell transparently regardless of CellType (Apache POI)
I want to control the start / stop of servers and databases with Alexa
What I tried when I wanted to get all the fields of a bean
I want to see the contents of Request without saying four or five
I want to call a method of another class
I want to know the answer of the rock-paper-scissors app
I want to display the name of the poster of the comment
I want to monitor a specific file with WatchService
[Rails] How to get the contents of strong parameters
I learned about the existence of a gemspec file
I want to return the scroll position of UITableView!
I want to add a delete function to the comment function
[Ruby] I want to make a program that displays today's day of the week!
[Rails] I want to display the link destination of link_to in a separate tab
Even if I want to convert the contents of a data object to JSON in Java, there is a circular reference ...
I want to find out which version of java the jar file I have is available
[Ruby on Rails] I want to get the URL of the image saved in Active Storage
When I switched to IntelliJ, I got a lot of differences in the encoding of the properties file.
I took a peek at the contents of Java's HashMap
[Rails] [bootstrap] I want to change the font size responsively
I tried to summarize the basics of kotlin and java
I want to bring Tomcat to the server and start the application
I want to expand the clickable part of the link_to method
I want to make a specific model of ActiveRecord ReadOnly
I want to change the log output settings of UtilLoggingJdbcLogger
I want to make a function with kotlin and java!
I tried JAX-RS and made a note of the procedure
I want to create a form to select the [Rails] category
[Ruby] How to retrieve the contents of a double hash
I want to control the maximum file size in file upload for each URL in Spring Boot
I want to give a class name to the select attribute
I want to create a Parquet file even in Ruby
I want to recursively search the class list under the package
How to change the contents of the jar file without decompressing
How to check the extension and size of uploaded files
I want to narrow down the display of docker ps
I want to recreate the contents of assets from scratch in the environment built with capistrano
[jsoup] How to get the full amount of a document
[Ruby] I want to reverse the order of the hash table
I want to temporarily disable the swipe gesture of UIPageViewController
[Controller] I want to retrieve the numerical value of a specific column from the DB (my memo)
I tried to express the result of before and after of Date class with a number line