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.
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);
}
}
}
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
I was able to expand it by dragging and dropping.
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.
Drag and drop the zip file
I was able to deploy it.
This source can be used if the path is java8 or higher.
Recommended Posts