フォルダ内のファイルを解凍します。
/**
* ファイル解凍処理
*/
private void deCompressFile() throws Exception {
// 圧縮ストリームを宣言する。
ZipInputStream zis = null;
try {
// 作業フォルダのファイルを解凍する。
zis = ZipInputStream(
new BufferedInputStream(
new FileInputStream(this.currentZipFileName)));
// ファイルのエントリを宣言する。
ZipEntry entry;
// ファイルに含まれるエントリ分繰り返す。
while ((entry = zis.getNextEntry()) != null) {
// 解凍対象ファイル名を取得する。
String fileName = entry.getName();
// ディレクトリの場合
if (entry.isDirectory()) {
// ディレクトリを作成する。
Path filePath = Paths.get(this.currentTmpDir + Const.FILE_PATH_SEPARATOR + fileName);
Files.createDirectories(filePath);
// ファイルの場合
} else {
// 解凍処理を行う。
extract(zis, fileName);
}
}
} catch (Exception e) {
LOGGER.error("エラー発生");
throw new Exception("エラー発生", e);
} finally {
if (zis != null) {
try {
zis.close();
} catch (Exception e) {
LOGGER.error("エラー発生");
throw new Exception("エラー発生", e);
}
}
}
}
* 解凍処理
* @param zis 圧縮ストリーム
* @param fileName 解凍対象ファイル名
*/
private void extract(ZipInputStream zis, String fileName) throws Exception {
// 出力用のストリームを宣言する。
BufferedOutputStream bos = null;
try {
// 解凍対象ファイルのフルパスを取得する。
Path filePath = Paths.get(this.currentTmpDir + Const.FILE_PATH_SEPARATOR + fileName);
// 解凍対象ファイルを読み込む。
int readSize = 0;
byte[] buffer = new byte[1024];
bos = new BufferedOutputStream(
new FileOutputStream(filePath.toString()), 1024);
// 解凍対象ファイルをファイルとして出力する。
while ((readSize = zis.read(buffer, 0, buffer.length)) != -1) {
bos.write(buffer, 0, readSize);
}
bos.flush();
} finally {
if (bos != null) {
bos.close();
}
zis.closeEntry();
}
}