use of org.apache.commons.compress.archivers.tar.TarArchiveEntry in project che by eclipse.
the class TarUtils method addFileEntry.
private static void addFileEntry(TarArchiveOutputStream tarOut, String entryName, File file, long modTime) throws IOException {
final TarArchiveEntry tarEntry = new TarArchiveEntry(file, entryName);
if (modTime >= 0) {
tarEntry.setModTime(modTime);
}
tarOut.putArchiveEntry(tarEntry);
try (InputStream in = new BufferedInputStream(new FileInputStream(file))) {
final byte[] buf = new byte[BUF_SIZE];
int r;
while ((r = in.read(buf)) != -1) {
tarOut.write(buf, 0, r);
}
}
tarOut.closeArchiveEntry();
}
use of org.apache.commons.compress.archivers.tar.TarArchiveEntry in project storm by apache.
the class Utils method unTarUsingJava.
private static void unTarUsingJava(File inFile, File untarDir, boolean gzipped) throws IOException {
InputStream inputStream = null;
try {
if (gzipped) {
inputStream = new BufferedInputStream(new GZIPInputStream(new FileInputStream(inFile)));
} else {
inputStream = new BufferedInputStream(new FileInputStream(inFile));
}
try (TarArchiveInputStream tis = new TarArchiveInputStream(inputStream)) {
for (TarArchiveEntry entry = tis.getNextTarEntry(); entry != null; ) {
unpackEntries(tis, entry, untarDir);
entry = tis.getNextTarEntry();
}
}
} finally {
if (inputStream != null) {
inputStream.close();
}
}
}
use of org.apache.commons.compress.archivers.tar.TarArchiveEntry in project storm by apache.
the class Utils method unpackEntries.
private static void unpackEntries(TarArchiveInputStream tis, TarArchiveEntry entry, File outputDir) throws IOException {
if (entry.isDirectory()) {
File subDir = new File(outputDir, entry.getName());
if (!subDir.mkdirs() && !subDir.isDirectory()) {
throw new IOException("Mkdirs failed to create tar internal dir " + outputDir);
}
for (TarArchiveEntry e : entry.getDirectoryEntries()) {
unpackEntries(tis, e, subDir);
}
return;
}
File outputFile = new File(outputDir, entry.getName());
if (!outputFile.getParentFile().exists()) {
if (!outputFile.getParentFile().mkdirs()) {
throw new IOException("Mkdirs failed to create tar internal dir " + outputDir);
}
}
int count;
byte[] data = new byte[2048];
BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(outputFile));
while ((count = tis.read(data)) != -1) {
outputStream.write(data, 0, count);
}
outputStream.flush();
outputStream.close();
}
use of org.apache.commons.compress.archivers.tar.TarArchiveEntry in project deeplearning4j 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")) {
//getFromOrigin the zip file content
ZipInputStream zis = new ZipInputStream(fin);
//getFromOrigin the zipped file list entry
ZipEntry ze = zis.getNextEntry();
while (ze != null) {
String fileName = ze.getName();
File newFile = new File(dest + File.separator + fileName);
log.info("file unzip : " + newFile.getAbsoluteFile());
//createComplex all non exists folders
//else you will hit FileNotFoundException for compressed folder
new File(newFile.getParent()).mkdirs();
FileOutputStream fos = new FileOutputStream(newFile);
int len;
while ((len = zis.read(data)) > 0) {
fos.write(data, 0, len);
}
fos.close();
ze = zis.getNextEntry();
}
zis.closeEntry();
zis.close();
} 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 = null;
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;
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")) {
GZIPInputStream is2 = new GZIPInputStream(fin);
File extracted = new File(target.getParent(), target.getName().replace(".gz", ""));
if (extracted.exists())
extracted.delete();
extracted.createNewFile();
OutputStream fos = FileUtils.openOutputStream(extracted);
IOUtils.copyLarge(is2, fos);
is2.close();
fos.flush();
fos.close();
}
target.delete();
}
use of org.apache.commons.compress.archivers.tar.TarArchiveEntry in project jstorm by alibaba.
the class Utils method unpackEntries.
private static void unpackEntries(TarArchiveInputStream tis, TarArchiveEntry entry, File outputDir) throws IOException {
if (entry.isDirectory()) {
File subDir = new File(outputDir, entry.getName());
if (!subDir.mkdirs() && !subDir.isDirectory()) {
throw new IOException("Mkdirs failed to create tar internal dir " + outputDir);
}
for (TarArchiveEntry e : entry.getDirectoryEntries()) {
unpackEntries(tis, e, subDir);
}
return;
}
File outputFile = new File(outputDir, entry.getName());
if (!outputFile.getParentFile().exists()) {
if (!outputFile.getParentFile().mkdirs()) {
throw new IOException("Mkdirs failed to create tar internal dir " + outputDir);
}
}
int count;
byte[] data = new byte[2048];
BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(outputFile));
while ((count = tis.read(data)) != -1) {
outputStream.write(data, 0, count);
}
outputStream.flush();
outputStream.close();
}
Aggregations