Skip to content

Commit

Permalink
#6686 ArchiveUtils listing, single file extraction for zip
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexDBlack committed Nov 17, 2018
1 parent e9e1fff commit 3e7ee68
Showing 1 changed file with 45 additions and 5 deletions.
50 changes: 45 additions & 5 deletions nd4j/nd4j-common/src/main/java/org/nd4j/util/ArchiveUtils.java
Expand Up @@ -29,9 +29,11 @@

import java.io.*;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.zip.GZIPInputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;

/**
Expand Down Expand Up @@ -96,11 +98,16 @@ public static void unzipFileTo(String file, String dest) throws IOException {

zis.closeEntry();
}
} else if (file.endsWith(".tar.gz") || file.endsWith(".tgz")) {

} else if (file.endsWith(".tar.gz") || file.endsWith(".tgz") || file.endsWith(".tar")) {
BufferedInputStream in = new BufferedInputStream(fin);
GzipCompressorInputStream gzIn = new GzipCompressorInputStream(in);
TarArchiveInputStream tarIn = new TarArchiveInputStream(gzIn);
TarArchiveInputStream tarIn;
if(file.endsWith(".tar")){
//Not compressed
tarIn = new TarArchiveInputStream(in);
} else {
GzipCompressorInputStream gzIn = new GzipCompressorInputStream(in);
tarIn = new TarArchiveInputStream(gzIn);
}

TarArchiveEntry entry;
/* Read the tar entries using the getNextEntry method **/
Expand Down Expand Up @@ -137,7 +144,7 @@ public static void unzipFileTo(String file, String dest) throws IOException {
if (extracted.exists())
extracted.delete();
extracted.createNewFile();
try(GZIPInputStream is2 = new GZIPInputStream(fin); OutputStream fos = FileUtils.openOutputStream(extracted)) {
try (GZIPInputStream is2 = new GZIPInputStream(fin); OutputStream fos = FileUtils.openOutputStream(extracted)) {
IOUtils.copyLarge(is2, fos);
fos.flush();
}
Expand Down Expand Up @@ -166,6 +173,39 @@ public static List<String> tarGzListFiles(File tarGzFile) throws IOException {
}
}

/**
* List all of the files and directories in the specified .zip file
*
* @param zipFile Zip file
* @return List of files and directories
*/
public static List<String> zipListFiles(File zipFile) throws IOException {
List<String> out = new ArrayList<>();
try (ZipFile zf = new ZipFile(zipFile)) {
Enumeration entries = zf.entries();
while (entries.hasMoreElements()) {
ZipEntry ze = (ZipEntry) entries.nextElement();
out.add(ze.getName());
}
}
return out;
}

/**
* Extract a single file from a .zip file. Does not support directories
*
* @param zipFile Zip file to extract from
* @param destination Destination file
* @param pathInZip Path in the zip to extract
* @throws IOException If exception occurs while reading/writing
*/
public static void zipExtractSingleFile(File zipFile, File destination, String pathInZip) throws IOException {
try (ZipFile zf = new ZipFile(zipFile); InputStream is = new BufferedInputStream(zf.getInputStream(zf.getEntry(pathInZip)));
OutputStream os = new BufferedOutputStream(new FileOutputStream(destination))) {
IOUtils.copy(is, os);
}
}

/**
* Extract a single file from a tar.gz file. Does not support directories.
* NOTE: This should not be used for batch extraction of files, due to the need to iterate over the entries until the
Expand Down

0 comments on commit 3e7ee68

Please sign in to comment.