The story I wanted to unzip

Trigger

When exchanging ZIP files at work, the ZIP creator was a Mac user and this was Windows. As you can see at this point, the entry name is ZIP-ized with UTF-8 on Mac. In the standard Windows ZIP deployment, the entry name is garbled because it is expanded in Windows-31j (Shift-JIS) in the Japanese environment. ~~ I still don't understand the meaning of having trouble with the character code. ~~

It's over when I put in free software that supports UTF-8, Since I work as a resident customer, I may download it without permission, and since it's a big deal, I decided to solve it myself.

Deploy in Java for the time being

Reference: http://www.ne.jp/asahi/hishidama/home/tech/java/zip.html It was almost solved on Mr. Hishidama's page of stability. ..

ZipDecoder.java


import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.zip.ZipFile;

public class ZipDecoder {

	private static final String ZIP_FILE = "C:\\temp\\folder.zip";

	public static void main(String[] args) {
		decode(ZIP_FILE);
	}

	public static void decode(String strPath) {
		if (strPath == null || !strPath.toLowerCase().endsWith(".zip")) {
			System.out.println("Argument is not zip file.");
			return;
		}
		Path zipFilePath = Paths.get(strPath);
		String outDir = zipFilePath.toString().replace(".zip", "");
		if (Files.notExists(zipFilePath)) {
			System.out.println("Argument is not file path.");
			return;
		}
		try (ZipFile zipFile = new ZipFile(zipFilePath.toFile(), Charset.forName("UTF-8"))) {
			zipFile.stream().filter(entry -> !entry.isDirectory()).forEach(entry -> {
				Path outName = Paths.get(outDir, entry.getName());
				try {
					Files.createDirectories(outName.getParent());
				} catch (IOException e1) {
					throw new UncheckedIOException(e1);
				}
				try (InputStream is = zipFile.getInputStream(entry);
						BufferedOutputStream os = new BufferedOutputStream(Files.newOutputStream(outName))) {
					byte[] buf = new byte[1024];
					while (is.read(buf) >= 0) {
						os.write(buf);
					}
				} catch (IOException e) {
					throw new UncheckedIOException(e);
				}
			});

		} catch (IOException e) {
			throw new UncheckedIOException(e);
		}
	}
}

Reuse

Since the exchange of ZIP files is still going on, I will recreate it so that it can be used. The file name should be passed as a startup argument so that it can be expanded by dragging and dropping. It seems that you can make a jar into an exe file by using something called launch4j, but since it is resident at the customer (

Reference: https://qiita.com/bugtrap/items/15864474076767a7a957

ZipDecoder.java


import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.zip.ZipFile;

public class ZipDecoder {

	public static void main(String[] args) {
		if (args.length == 0) {
			System.out.println("There are no Arguments.");
		}
		Arrays.asList(args).stream().forEach(ZipDecoder::decode);
	}

	public static void decode(String strPath) {
		if (strPath == null || !strPath.toLowerCase().endsWith(".zip")) {
			System.out.println("Argument is not zip file.");
			return;
		}
		Path zipFilePath = Paths.get(strPath);
		String outDir = zipFilePath.toString().replace(".zip", "");
		if (Files.notExists(zipFilePath)) {
			System.out.println("Argument is not file path.");
			return;
		}
		try (ZipFile zipFile = new ZipFile(zipFilePath.toFile(), Charset.forName("UTF-8"))) {
			zipFile.stream().filter(entry -> !entry.isDirectory()).forEach(entry -> {
				Path outName = Paths.get(outDir, entry.getName());
				try {
					Files.createDirectories(outName.getParent());
				} catch (IOException e1) {
					throw new UncheckedIOException(e1);
				}
				try (InputStream is = zipFile.getInputStream(entry);
						BufferedOutputStream os = new BufferedOutputStream(Files.newOutputStream(outName))) {
					byte[] buf = new byte[1024];
					while (is.read(buf) >= 0) {
						os.write(buf);
					}
				} catch (IOException e) {
					throw new UncheckedIOException(e);
				}
			});

		} catch (IOException e) {
			throw new UncheckedIOException(e);
		}
	}
}

call-decoder.bat


@echo off

rem command check
where java 2> nul > nul || goto :notfound

java -jar ZipDecoder.jar %*

echo Deployment completed
goto :end

:notfound
put echo java command in PATH
goto :end

:end
pause

Select two zip files and drag and drop them into the bat file

image.png

I was able to expand it by dragging and dropping.

image.png

Troublesome to have two java file and bat file

Why not create a java file in bat, compile it, and use it? So I tried it.

decord.bat


@echo off

rem command check
where java 2> nul > nul || goto :notfound

echo import java.io.BufferedOutputStream;                                                                         > ZipDecoder.java
echo import java.io.IOException;                                                                                  >> ZipDecoder.java
echo import java.io.InputStream;                                                                                  >> ZipDecoder.java
echo import java.io.UncheckedIOException;                                                                         >> ZipDecoder.java
echo import java.nio.charset.Charset;                                                                             >> ZipDecoder.java
echo import java.nio.file.Files;                                                                                  >> ZipDecoder.java
echo import java.nio.file.Path;                                                                                   >> ZipDecoder.java
echo import java.nio.file.Paths;                                                                                  >> ZipDecoder.java
echo import java.util.Arrays;                                                                                     >> ZipDecoder.java
echo import java.util.zip.ZipFile;                                                                                >> ZipDecoder.java
echo public class ZipDecoder {                                                                                    >> ZipDecoder.java
echo 	public static void main(String[] args) {                                                                  >> ZipDecoder.java
echo 		if (args.length == 0) {                                                                               >> ZipDecoder.java
echo 			System.out.println("There are no Arguments.");                                                    >> ZipDecoder.java
echo 		}                                                                                                     >> ZipDecoder.java
echo 		Arrays.asList(args).stream().forEach(ZipDecoder::decode);                                             >> ZipDecoder.java
echo 	}                                                                                                         >> ZipDecoder.java
echo 	public static void decode(String strPath) {                                                               >> ZipDecoder.java
echo 		if (strPath == null ^|^| !strPath.toLowerCase().endsWith(".zip")) {                                     >> ZipDecoder.java
echo 			System.out.println("Argument is not zip file.");                                                  >> ZipDecoder.java
echo 			return;                                                                                           >> ZipDecoder.java
echo 		}                                                                                                     >> ZipDecoder.java
echo 		Path zipFilePath = Paths.get(strPath);                                                                >> ZipDecoder.java
echo 		String outDir = zipFilePath.toString().replace(".zip", "");                                           >> ZipDecoder.java
echo 		if (Files.notExists(zipFilePath)) {                                                                   >> ZipDecoder.java
echo 			System.out.println("Argument is not file path.");                                                 >> ZipDecoder.java
echo 			return;                                                                                           >> ZipDecoder.java
echo 		}                                                                                                     >> ZipDecoder.java
echo 		try (ZipFile zipFile = new ZipFile(zipFilePath.toFile(), Charset.forName("UTF-8"))) {                 >> ZipDecoder.java
echo 			zipFile.stream().filter(entry -^> !entry.isDirectory()).forEach(entry -^> {                         >> ZipDecoder.java
echo 				Path outName = Paths.get(outDir, entry.getName());                                            >> ZipDecoder.java
echo 				try {                                                                                         >> ZipDecoder.java
echo 					Files.createDirectories(outName.getParent());                                             >> ZipDecoder.java
echo 				} catch (IOException e1) {                                                                    >> ZipDecoder.java
echo 					throw new UncheckedIOException(e1);                                                       >> ZipDecoder.java
echo 				}                                                                                             >> ZipDecoder.java
echo 				try (InputStream is = zipFile.getInputStream(entry);                                          >> ZipDecoder.java
echo 						BufferedOutputStream os = new BufferedOutputStream(Files.newOutputStream(outName))) { >> ZipDecoder.java
echo 					byte[] buf = new byte[1024];                                                              >> ZipDecoder.java
echo 					while (is.read(buf) ^>= 0) {                                                               >> ZipDecoder.java
echo 						os.write(buf);                                                                        >> ZipDecoder.java
echo 					}                                                                                         >> ZipDecoder.java
echo 				} catch (IOException e) {                                                                     >> ZipDecoder.java
echo 					throw new UncheckedIOException(e);                                                        >> ZipDecoder.java
echo 				}                                                                                             >> ZipDecoder.java
echo 			});                                                                                               >> ZipDecoder.java
echo 		} catch (IOException e) {                                                                             >> ZipDecoder.java
echo 			throw new UncheckedIOException(e);                                                                >> ZipDecoder.java
echo 		}                                                                                                     >> ZipDecoder.java
echo 	}                                                                                                         >> ZipDecoder.java
echo }                                                                                                            >> ZipDecoder.java

javac ZipDecoder.java

java ZipDecoder %*

del ZipDecoder.java
del ZipDecoder.class

echo Deployment completed
goto :end

:notfound
put echo java command in PATH
goto :end

:end
pause

After escaping some symbols, I was able to output normally.

image.png

Drag and drop the zip file

image.png

I was able to deploy it.

Supplement

This source can be used if the path is java8 or higher.

Recommended Posts

The story I wanted to unzip
I wanted to add @VisibleForTesting to the method
I tried to explain the method
The story I was addicted to when setting up STS
I wanted to make JavaFX programming easier with the Spring Framework
I tried to summarize the methods used
I just wanted to logrotate with log4j 2
I was addicted to the roll method
I tried to implement the Iterator pattern
I was addicted to the Spring-Batch test
I tried to summarize the Stream API
I went to the Java Women's Club # 1
It was a life I wanted to reset the thread-safe associative counter
After all I wanted to preview the contents of mysql with Docker ...
A story that I wanted to write a process equivalent to a while statement with the Stream API of Java8
[Rails] I tried to raise the Rails version from 5.0 to 5.2
I tried to organize the session in Rails
The story of introducing Ajax communication to ruby
The story of raising Spring Boot 1.5 series to 2.1 series
I want to var_dump the contents of the intent
I wanted to make (a == 1 && a == 2 && a == 3) true in Java
The code I used to connect Rails 3 to PostgreSQL 10
The story of adding the latest Node.js to DockerFile
I tried to set tomcat to run the Servlet.
I want to truncate after the decimal point
I wanted to gradle spring boot with multi-project
I want to get the value in Ruby
What I tried when I wanted to get all the fields of a bean
[Small story] I tried to make the java ArrayList a little more convenient
I wanted to start the AP server and debug with just the Maven command
I was addicted to the NoSuchMethodError in Cloud Endpoints
I tried what I wanted to try with Stream softly.
I tried to organize the cases used in programming
[Java] I want to calculate the difference from the date
I want to embed any TraceId in the log
Tokoro I rewrote in the migration from Wicket 7 to 8
I was addicted to the record of the associated model
I tried to summarize the state transition of docker
I tried to decorate the simple calendar a little
The story of migrating from Paperclip to Active Storage
05. I tried to stub the source of Spring Boot
I want to judge the range using the monthly degree
What should I do to reload the updated Dockerfile?
I want to know the answer of the rock-paper-scissors app
[Rails] I don't know how to use the model ...
I want to display the name of the poster of the comment
I want to dark mode with the SWT app
I want to call the main method using reflection
I wanted to develop PHP with vscode remote container
[Rough commentary] I want to marry the pluck method
I tried to implement the Euclidean algorithm in Java
I want to be aware of the contents of variables!
I want to return the scroll position of UITableView!
I want to simplify the log output on Android
I want to add a delete function to the comment function
A story that I realized that I had to study as an engineer in the first place
I can't find the docker image after updating to docker desktop 2.4.0.0
The story of raising Spring Boot from 1.5 series to 2.1 series part2
I tried to implement the like function by asynchronous communication
[Beginner] I want to modify the migration file-How to use rollback-
I tried to introduce Bootstrap 4 to the Rails 6 app [for beginners]