私のプログラムでこのようなことをしたいのですが:
File zipFile = .....;
File destDir = ....;
ImaginaryZipUtility.unzipAllTo(zipFile, destdir);
私はおそらくプログラムからこれを行う最初の人になることはできません。上記のようなユーティリティメソッドはどこにありますか?私はapachecommons-ioを見ようとしましたが、そこには何もありません。だから、どこを見ればいいの?
私が掘り起こすことができた非常に古いコード
package com.den.frontend;
import java.util.*;
import java.util.zip.*;
import java.io.*;
public class ZIPUtility
{
public static final int BUFFER_SIZE = 2048;
//This function converts the zip file into uncompressed files which are placed in the
//destination directory
//destination directory should be created first
public static boolean unzipFiles(String srcDirectory, String srcFile, String destDirectory)
{
try
{
//first make sure that all the arguments are valid and not null
if(srcDirectory == null)
{
System.out.println(1);
return false;
}
if(srcFile == null)
{
System.out.println(2);
return false;
}
if(destDirectory == null)
{
System.out.println(3);
return false;
}
if(srcDirectory.equals(""))
{
System.out.println(4);
return false;
}
if(srcFile.equals(""))
{
System.out.println(5);
return false;
}
if(destDirectory.equals(""))
{
System.out.println(6);
return false;
}
//now make sure that these directories exist
File sourceDirectory = new File(srcDirectory);
File sourceFile = new File(srcDirectory + File.separator + srcFile);
File destinationDirectory = new File(destDirectory);
// Prevent "Zip Slip" vulnerability https://snyk.io/research/zip-slip-vulnerability
String canonicalDestinationFile = sourceFile.getCanonicalPath();
if (!canonicalDestinationFile.startsWith(destinationDirectory.getCanonicalPath() + File.separator)) {
throw new Exception("Entry is outside of the target dir: " + e.getName());
}
if(!sourceDirectory.exists())
{
System.out.println(7);
return false;
}
if(!sourceFile.exists())
{
System.out.println(sourceFile);
return false;
}
if(!destinationDirectory.exists())
{
System.out.println(9);
return false;
}
//now start with unzip process
BufferedOutputStream dest = null;
FileInputStream fis = new FileInputStream(sourceFile);
ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
ZipEntry entry = null;
while((entry = zis.getNextEntry()) != null)
{
String outputFilename = destDirectory + File.separator + entry.getName();
System.out.println("Extracting file: " + entry.getName());
createDirIfNeeded(destDirectory, entry);
int count;
byte data[] = new byte[BUFFER_SIZE];
//write the file to the disk
FileOutputStream fos = new FileOutputStream(outputFilename);
dest = new BufferedOutputStream(fos, BUFFER_SIZE);
while((count = zis.read(data, 0, BUFFER_SIZE)) != -1)
{
dest.write(data, 0, count);
}
//close the output streams
dest.flush();
dest.close();
}
//we are done with all the files
//close the zip file
zis.close();
}
catch(Exception e)
{
e.printStackTrace();
return false;
}
return true;
}
private static void createDirIfNeeded(String destDirectory, ZipEntry entry)
{
String name = entry.getName();
if(name.contains("/"))
{
System.out.println("directory will need to be created");
int index = name.lastIndexOf("/");
String dirSequence = name.substring(0, index);
File newDirs = new File(destDirectory + File.separator + dirSequence);
//create the directory
newDirs.mkdirs();
}
}
}
私はショーにかなり遅れていることを知っていますが、私は同じものを探していて、グーグルでこれを見つけました。私が見つけた最も簡単な方法は、アリのタスク「解凍」です。これを含めることで、Javaソースから複雑なantセットアップ(完全なantプロジェクトを構築するためのProjectHelperのようなものがあります)なしでそれを使用できます。
<dependency>
<groupId>org.apache.ant</groupId>
<artifactId>ant-compress</artifactId>
<version>1.2</version>
</dependency>
ファイルをディレクトリに解凍するコードは次のようになります
org.apache.ant.compress.taskdefs.Unzip u = new Unzip();
u.setSrc(new File("<archive.zip>"));
u.setDest(new File("<targetDir>"));
u.execute();
そうですね、java.util.zipには、非常に簡単な方法で必要なことを実行するためのクラスがあります。
TrueZipライブラリを使用してやりたいことができるようです: https ://truezip.dev.java.net/manual-6.html#Copying
ライブラリは非常に大きく、必要以上にスコープが広いため、これは理想的な状況ではありません(また、java.io.Fileのサブクラス(で使用するためのファイルとも呼ばれます)の周りに編成されるなど、いくつかの独特で紛らわしい詳細があります通常、java.io.Fileインスタンスも処理するクラス!)。
少なくとも、クラス内のコード行の大部分がクラスの責任とは無関係である状況、またはモジュールの目的とは無関係なプロジェクト内の複雑なユーティリティクラスを維持する状況にある必要はありません。 。
これは、経験豊富な開発者がJavaからRubyに移行する主な理由の典型的な例だと思います。Javaには豊富なライブラリがありますが、それらの多くは設計が不十分であるため、単純な操作は、より特殊な操作と同じように実行が困難になります。日常のタスクを単純にするよりも、すべての詳細と可能性を明らかにすることに熱心なテクノロジーの専門家によって、ボトムアップで書かれているようです。Apache Commonsの人々は、クラスのビジネス目的とは関係のないコード行、特にループや条件からクラスを解放するライブラリを作成することを光栄に思います。