Unzip the zip file in Java

Since I had the opportunity to unzip the zip file with Java, it will be a technical memo at that time (´ ・ ω ・ `) First of all, the three files to be stored in the zip (ʻa.txt, dir1 / b" Create .txt, dir1 / dir2 / c.txt`).

mkdir -p dir1/dir2
touch a.txt dir1/b.txt dir1/dir2/c.txt
echo "AAAAAAAAAA" > a.txt
echo "BBBBBBBBBB" > dir1/b.txt
echo "CCCCCCCCCC" > dir1/dir2/c.txt

You can check the created contents with the following command.

find . -name "*.txt" | while read path; do
  echo "---> ${path} <---"
  cat ${path}
done
---> ./a.txt <---
AAAAAAAAAA
---> ./dir1/b.txt <---
BBBBBBBBBB
---> ./dir1/dir2/c.txt <---
CCCCCCCCCC

Now that you have confirmed that the three files have been created as expected, compress them as a text.zip file.

$ zip "text.zip" $(find . -name "*.txt")
  adding: a.txt (deflated 45%)
  adding: dir1/b.txt (deflated 45%)
  adding: dir1/dir2/c.txt (deflated 45%)

Finally, let's confirm that the text.zip file was created successfully.

$ unzip -l text.zip
Archive:  text.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
       11  2018-08-24 23:17   a.txt
       11  2018-08-24 23:17   dir1/b.txt
       11  2018-08-24 23:17   dir1/dir2/c.txt
---------                     -------
       33                     3 files

The introduction has been lengthened, but if you want to unzip the text.zip file into the text directory, the Java code to do that is:

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class Main {
    public static void main(String[] args) throws IOException {
        var target = Paths.get("text");
        Files.createDirectories(target);
        
        var zipfile = Paths.get("text.zip");
        try (var in = new ZipInputStream(Files.newInputStream(zipfile))) {
            ZipEntry e;
            while ((e = in.getNextEntry()) != null) {
                var dst = Paths.get(target.toString(), e.getName());
                Files.createDirectories(dst.getParent());
                Files.write(dst, in.readAllBytes());
                System.out.printf("inflating: %s%n", dst);
            }
        }
    }
}

When you run this Java code, the text.zip will be unzipped into the text directory and you should see a log similar to the following in standard output.

inflating: text\a.txt
inflating: text\dir1\b.txt
inflating: text\dir1\dir2\c.txt

So was the text.zip file really unzipped under the text directory? You can check this with the following command.

$ find ./text -name "*.txt" | while read path; do
>   echo "---> ${path} <---"
>   cat ${path}
> done
---> ./text/a.txt <---
AAAAAAAAAA
---> ./text/dir1/b.txt <---
BBBBBBBBBB
---> ./text/dir1/dir2/c.txt <---
CCCCCCCCCC

Recommended Posts

Unzip the zip file in Java
Sample to unzip gz file in Java
[Java] Read the file in src / main / resources
[Java] Get the file in the jar regardless of the environment
[Java] Get the file path in the folder with List
Access the network interface in Java
Guess the character code in Java
Specify the java location in eclipse.ini
Log output to file in Java
Parsing the COTOHA API in Java
About file copy processing in Java
Call the super method in Java
Get the public URL of a private Flickr file in Java
How to get the length of an audio file in java
[Java] How to use the File class
Java reference to understand in the figure
Try using the Stream API in Java
Call the Windows Notification API in Java
I tried the new era in Java
[Java] Use cryptography in the standard library
Organized memo in the head (Java --Array)
What is the best file reading (Java)
Try calling the CORBA service in Java 11+
What is the main method in Java?
Read xlsx file in Java with Selenium
How to get the date in java
The story of writing Java in Emacs
Console input in Java (understanding the mechanism)
Java: Place the ResourceBundle properties file anywhere
[Java8] Search the directory and get the file
For the time being, run the war file delivered in Java with Docker
Partization in Java
To create a Zip file while grouping database search results in Java
Changes in Java 11
Memo: [Java] If a file is in the monitored directory, process it.
Rock-paper-scissors in Java
java file creation
Sample program that returns the hash value of a file in Java
Pi in Java
FizzBuzz in Java
Java that outputs vmg format file in eml format
Regarding the transient modifier and serialization in Java
The story of low-level string comparison in Java
JAVA: jar, aar, view the contents of the file
About the confusion seen in startup Java servers
The story of making ordinary Othello in Java
Read a string in a PDF file with Java
About the idea of anonymous classes in Java
ChatWork4j for using the ChatWork API in Java
A story about the JDK in the Java 11 era
Organized memo in the head (Java --Control syntax)
The intersection type introduced in Java 10 is amazing (?)
The story of learning Java in the first programming
Measure the size of a folder in Java
Feel the passage of time even in Java
Organized memo in the head (Java --instance edition)
A bat file that uses Java in windows
Organized memo in the head (Java --Data type)
Display "Hello World" in the browser using Java
[Java] Judgment by entering characters in the terminal
Display "Hello World" in the browser using Java