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