use of org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream in project AmazeFileManager by TeamAmaze.
the class GzipExtractor method extractWithFilter.
@Override
protected void extractWithFilter(@NonNull Filter filter) throws IOException {
long totalBytes = 0;
ArrayList<TarArchiveEntry> archiveEntries = new ArrayList<>();
TarArchiveInputStream inputStream = new TarArchiveInputStream(new GzipCompressorInputStream(new FileInputStream(filePath)));
TarArchiveEntry tarArchiveEntry;
while ((tarArchiveEntry = inputStream.getNextTarEntry()) != null) {
if (filter.shouldExtract(tarArchiveEntry.getName(), tarArchiveEntry.isDirectory())) {
archiveEntries.add(tarArchiveEntry);
totalBytes += tarArchiveEntry.getSize();
}
}
listener.onStart(totalBytes, archiveEntries.get(0).getName());
inputStream.close();
inputStream = new TarArchiveInputStream(new GzipCompressorInputStream(new FileInputStream(filePath)));
for (TarArchiveEntry entry : archiveEntries) {
if (!listener.isCancelled()) {
listener.onUpdate(entry.getName());
// TAR is sequential, you need to walk all the way to the file you want
while (entry.hashCode() != inputStream.getNextTarEntry().hashCode()) ;
extractEntry(context, inputStream, entry, outputPath);
}
}
inputStream.close();
listener.onFinish();
}
use of org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream in project nd4j by deeplearning4j.
the class ArchiveUtils method unzipFileTo.
/**
* Extracts files to the specified destination
*
* @param file the file to extract to
* @param dest the destination directory
* @throws IOException
*/
public static void unzipFileTo(String file, String dest) throws IOException {
File target = new File(file);
if (!target.exists())
throw new IllegalArgumentException("Archive doesnt exist");
FileInputStream fin = new FileInputStream(target);
int BUFFER = 2048;
byte[] data = new byte[BUFFER];
if (file.endsWith(".zip") || file.endsWith(".jar")) {
try (ZipInputStream zis = new ZipInputStream(fin)) {
// get the zipped file list entry
ZipEntry ze = zis.getNextEntry();
while (ze != null) {
String fileName = ze.getName();
File newFile = new File(dest + File.separator + fileName);
if (ze.isDirectory()) {
newFile.mkdirs();
zis.closeEntry();
ze = zis.getNextEntry();
continue;
}
FileOutputStream fos = new FileOutputStream(newFile);
int len;
while ((len = zis.read(data)) > 0) {
fos.write(data, 0, len);
}
fos.close();
ze = zis.getNextEntry();
log.debug("File extracted: " + newFile.getAbsoluteFile());
}
zis.closeEntry();
}
} else if (file.endsWith(".tar.gz") || file.endsWith(".tgz")) {
BufferedInputStream in = new BufferedInputStream(fin);
GzipCompressorInputStream gzIn = new GzipCompressorInputStream(in);
TarArchiveInputStream tarIn = new TarArchiveInputStream(gzIn);
TarArchiveEntry entry;
/* Read the tar entries using the getNextEntry method **/
while ((entry = (TarArchiveEntry) tarIn.getNextEntry()) != null) {
log.info("Extracting: " + entry.getName());
if (entry.isDirectory()) {
File f = new File(dest + File.separator + entry.getName());
f.mkdirs();
} else /*
* If the entry is a file,write the decompressed file to the disk
* and close destination stream.
*/
{
int count;
try (FileOutputStream fos = new FileOutputStream(dest + File.separator + entry.getName());
BufferedOutputStream destStream = new BufferedOutputStream(fos, BUFFER)) {
while ((count = tarIn.read(data, 0, BUFFER)) != -1) {
destStream.write(data, 0, count);
}
destStream.flush();
IOUtils.closeQuietly(destStream);
}
}
}
// Close the input stream
tarIn.close();
} else if (file.endsWith(".gz")) {
File extracted = new File(target.getParent(), target.getName().replace(".gz", ""));
if (extracted.exists())
extracted.delete();
extracted.createNewFile();
try (GZIPInputStream is2 = new GZIPInputStream(fin);
OutputStream fos = FileUtils.openOutputStream(extracted)) {
IOUtils.copyLarge(is2, fos);
fos.flush();
}
} else {
throw new IllegalStateException("Unable to infer file type (compression format) from source file name: " + file);
}
target.delete();
}
use of org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream in project systemml by apache.
the class ValidateLicAndNotice method getFilesFromTGZ.
/**
* This will return the list of files from tgz file.
*
* @param tgzFileName is the name of tgz file from which list of files with specified file extension will be returned.
* @param fileExt is the file extension to be used to get list of files to be returned.
* @return Returns list of files having specified extension from tgz file.
*/
public static List<String> getFilesFromTGZ(String tgzFileName, String fileExt) {
TarArchiveInputStream tarIn = null;
try {
tarIn = new TarArchiveInputStream(new GzipCompressorInputStream(new BufferedInputStream(new FileInputStream(tgzFileName))));
} catch (Exception e) {
Utility.debugPrint(Constants.DEBUG_ERROR, "Exception in unzipping tar file: " + e);
return null;
}
List<String> files = new ArrayList<String>();
try {
TarArchiveEntry tarEntry = null;
while ((tarEntry = tarIn.getNextTarEntry()) != null) {
if (tarEntry.getName().endsWith("." + fileExt)) {
int iPos = tarEntry.getName().lastIndexOf("/");
if (iPos == 0)
--iPos;
String strFileName = tarEntry.getName().substring(iPos + 1);
files.add(strFileName);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return (files);
}
use of org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream in project systemml by apache.
the class ValidateLicAndNotice method extractFileFromTGZ.
/**
* This will return the file from tgz file and store it in specified location.
*
* @param tgzFileName is the name of tgz file from which file to be extracted.
* @param fileName is the name of the file to be extracted.
* @param strDestLoc is the location where file will be extracted.
* @param bFirstDirLevel to indicate to get file from first directory level.
* @return Sucess or Failure
*/
public static boolean extractFileFromTGZ(String tgzFileName, String fileName, String strDestLoc, boolean bFirstDirLevel) {
boolean bRetCode = Constants.bFAILURE;
TarArchiveInputStream tarIn = null;
try {
tarIn = new TarArchiveInputStream(new GzipCompressorInputStream(new BufferedInputStream(new FileInputStream(tgzFileName))));
} catch (Exception e) {
Utility.debugPrint(Constants.DEBUG_ERROR, "Exception in unzipping tar file: " + e);
return bRetCode;
}
try {
BufferedOutputStream bufOut = null;
BufferedInputStream bufIn = null;
TarArchiveEntry tarEntry = null;
while ((tarEntry = tarIn.getNextTarEntry()) != null) {
if (!tarEntry.getName().endsWith(fileName))
continue;
// Get file at root (in single directory) level. This is for License in root location.
if (bFirstDirLevel && (tarEntry.getName().indexOf('/') != tarEntry.getName().lastIndexOf('/')))
continue;
bufIn = new BufferedInputStream(tarIn);
int count;
byte[] data = new byte[Constants.BUFFER];
String strOutFileName = strDestLoc == null ? tarEntry.getName() : strDestLoc + "/" + fileName;
FileOutputStream fos = new FileOutputStream(strOutFileName);
bufOut = new BufferedOutputStream(fos, Constants.BUFFER);
while ((count = bufIn.read(data, 0, Constants.BUFFER)) != -1) {
bufOut.write(data, 0, count);
}
bufOut.flush();
bufOut.close();
bufIn.close();
bRetCode = Constants.bSUCCESS;
break;
}
} catch (Exception e) {
e.printStackTrace();
}
return bRetCode;
}
use of org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream in project incubator-gobblin by apache.
the class StreamUtilsTest method testTarDir.
@Test
public void testTarDir() throws IOException {
FileSystem localFs = FileSystem.getLocal(new Configuration());
// Set of expected Paths to be in the resulting tar file
Set<Path> expectedPaths = Sets.newHashSet();
// Create input directory
Path testInDir = new Path("testDir");
expectedPaths.add(testInDir);
// Create output file path
Path testOutFile = new Path("testTarOut" + UUID.randomUUID() + ".tar.gz");
try {
localFs.mkdirs(testInDir);
// Create a test file path
Path testFile1 = new Path(testInDir, "testFile1");
expectedPaths.add(testFile1);
FSDataOutputStream testFileOut1 = localFs.create(testFile1);
testFileOut1.close();
// Create a test file path
Path testFile2 = new Path(testInDir, "testFile2");
expectedPaths.add(testFile2);
FSDataOutputStream testFileOut2 = localFs.create(testFile2);
testFileOut2.close();
// tar the input directory to the specific output file
StreamUtils.tar(localFs, testInDir, testOutFile);
// Confirm the contents of the tar file are valid
try (TarArchiveInputStream tarArchiveInputStream = new TarArchiveInputStream(new GzipCompressorInputStream(localFs.open(testOutFile)))) {
TarArchiveEntry tarArchiveEntry;
while (null != (tarArchiveEntry = tarArchiveInputStream.getNextTarEntry())) {
assertThat(new Path(tarArchiveEntry.getName()), isIn(expectedPaths));
}
}
} finally {
if (localFs.exists(testInDir)) {
localFs.delete(testInDir, true);
}
if (localFs.exists(testOutFile)) {
localFs.delete(testOutFile, true);
}
}
}
Aggregations