8: Create ZipTool
This commit is contained in:
@@ -21,6 +21,7 @@ dependencies {
|
||||
compile "commons-cli:commons-cli:${commonCliVersion}"
|
||||
compile "io.vavr:vavr:${vavrVersion}"
|
||||
compile "com.github.javaparser:javaparser-core:${javaParserVersion}"
|
||||
compile "commons-io:commons-io:${commonsIoVersion}"
|
||||
}
|
||||
|
||||
jar {
|
||||
|
||||
@@ -4,4 +4,5 @@ ext {
|
||||
commonCliVersion = '1.4'
|
||||
vavrVersion = '1.0.0-alpha-2'
|
||||
javaParserVersion = '3.13.4'
|
||||
commonsIoVersion = '2.6'
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package com.bartek.esa.file.di;
|
||||
import com.bartek.esa.file.matcher.GlobMatcher;
|
||||
import com.bartek.esa.file.provider.FileContentProvider;
|
||||
import com.bartek.esa.file.provider.FileProvider;
|
||||
import com.bartek.esa.file.zip.ZipTool;
|
||||
import dagger.Module;
|
||||
import dagger.Provides;
|
||||
|
||||
@@ -23,4 +24,9 @@ public class FileModule {
|
||||
public GlobMatcher globMatcher() {
|
||||
return new GlobMatcher();
|
||||
}
|
||||
|
||||
@Provides
|
||||
public ZipTool zipTool() {
|
||||
return new ZipTool();
|
||||
}
|
||||
}
|
||||
|
||||
59
src/main/java/com/bartek/esa/file/zip/ZipTool.java
Normal file
59
src/main/java/com/bartek/esa/file/zip/ZipTool.java
Normal file
@@ -0,0 +1,59 @@
|
||||
package com.bartek.esa.file.zip;
|
||||
|
||||
import com.bartek.esa.error.EsaException;
|
||||
import io.vavr.control.Try;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import java.io.*;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipFile;
|
||||
|
||||
public class ZipTool {
|
||||
|
||||
@Inject
|
||||
public ZipTool() {
|
||||
|
||||
}
|
||||
|
||||
public void unzipArchive(File archive, File outputDir) {
|
||||
Try.run(() -> tryToUnzipArchive(archive, outputDir))
|
||||
.getOrElseThrow(EsaException::new);
|
||||
}
|
||||
|
||||
private void tryToUnzipArchive(File archive, File outputDir) throws IOException {
|
||||
ZipFile zipFile = new ZipFile(archive);
|
||||
|
||||
zipFile.stream().forEach(entry -> unzipEntry(zipFile, entry, outputDir));
|
||||
}
|
||||
|
||||
private void unzipEntry(ZipFile zipfile, ZipEntry entry, File outputDir) {
|
||||
if (entry.isDirectory()) {
|
||||
createDir(new File(outputDir, entry.getName()));
|
||||
return;
|
||||
}
|
||||
|
||||
File outputFile = new File(outputDir, entry.getName());
|
||||
if (!outputFile.getParentFile().exists()){
|
||||
createDir(outputFile.getParentFile());
|
||||
}
|
||||
|
||||
BufferedInputStream inputStream = Try.of(() -> new BufferedInputStream(zipfile.getInputStream(entry)))
|
||||
.getOrElseThrow(EsaException::new);
|
||||
BufferedOutputStream outputStream = Try.of(() -> new BufferedOutputStream(new FileOutputStream(outputFile)))
|
||||
.getOrElseThrow(EsaException::new);
|
||||
|
||||
try {
|
||||
IOUtils.copy(inputStream, outputStream);
|
||||
} catch (IOException e) {
|
||||
throw new EsaException(e);
|
||||
} finally {
|
||||
Try.run(outputStream::close).getOrElseThrow(EsaException::new);
|
||||
Try.run(inputStream::close).getOrElseThrow(EsaException::new);
|
||||
}
|
||||
}
|
||||
|
||||
private void createDir(File dir) {
|
||||
if(!dir.mkdirs()) throw new EsaException("Cannot create directory " + dir);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user