use of org.apache.commons.compress.archivers.tar.TarArchiveInputStream in project AmazeFileManager by TeamAmaze.
the class TarExtractor method extractWithFilter.
@Override
protected void extractWithFilter(@NonNull Filter filter) throws IOException {
long totalBytes = 0;
ArrayList<TarArchiveEntry> archiveEntries = new ArrayList<>();
TarArchiveInputStream inputStream = new TarArchiveInputStream(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 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.archivers.tar.TarArchiveInputStream in project docker-client by spotify.
the class DefaultDockerClientTest method testCopyContainer.
@Test
@SuppressWarnings("deprecation")
public void testCopyContainer() throws Exception {
requireDockerApiVersionLessThan("1.24", "failCopyToContainer");
// Pull image
sut.pull(BUSYBOX_LATEST);
// Create container
final ContainerConfig config = ContainerConfig.builder().image(BUSYBOX_LATEST).build();
final String name = randomName();
final ContainerCreation creation = sut.createContainer(config, name);
final String id = creation.id();
final ImmutableSet.Builder<String> files = ImmutableSet.builder();
try (final TarArchiveInputStream tarStream = new TarArchiveInputStream(sut.copyContainer(id, "/bin"))) {
TarArchiveEntry entry;
while ((entry = tarStream.getNextTarEntry()) != null) {
files.add(entry.getName());
}
}
// Check that some common files exist
assertThat(files.build(), both(hasItem("bin/")).and(hasItem("bin/wc")));
}
use of org.apache.commons.compress.archivers.tar.TarArchiveInputStream in project vespa by vespa-engine.
the class CompressedApplicationInputStreamTest method require_that_gnu_tared_file_can_be_unpacked.
@Test
public void require_that_gnu_tared_file_can_be_unpacked() throws IOException, InterruptedException {
File tmpTar = File.createTempFile("myapp", ".tar");
Process p = new ProcessBuilder("tar", "-C", "src/test/resources/deploy/validapp", "--exclude=.svn", "-cvf", tmpTar.getAbsolutePath(), ".").start();
p.waitFor();
p = new ProcessBuilder("gzip", tmpTar.getAbsolutePath()).start();
p.waitFor();
File gzFile = new File(tmpTar.getAbsolutePath() + ".gz");
assertTrue(gzFile.exists());
CompressedApplicationInputStream unpacked = CompressedApplicationInputStream.createFromCompressedStream(new TarArchiveInputStream(new GZIPInputStream(new FileInputStream(gzFile))));
File outApp = unpacked.decompress();
assertTestApp(outApp);
}
use of org.apache.commons.compress.archivers.tar.TarArchiveInputStream 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.archivers.tar.TarArchiveInputStream 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);
}
Aggregations