use of org.apache.commons.compress.archivers.tar.TarArchiveEntry in project quickutil by quickutil.
the class CompressUtil method decompressTarGz.
/**
* 解压tar.gz文件:
*
* @return 解压后的根目录路径
*/
public static String decompressTarGz(String sourcePath, String targetPath) {
String rootPath = null;
try (FileInputStream fInput = new FileInputStream(sourcePath);
BufferedInputStream bufInput = new BufferedInputStream(fInput);
GZIPInputStream gzipInput = new GZIPInputStream(bufInput);
ArchiveInputStream archiveInput = new ArchiveStreamFactory().createArchiveInputStream("tar", gzipInput)) {
// tar压缩文件条目
TarArchiveEntry entry;
boolean isRootPath = true;
while ((entry = (TarArchiveEntry) archiveInput.getNextEntry()) != null) {
String entryName = entry.getName();
// 转换为目标路径
if (targetPath != null) {
entryName = targetPath + File.separator + entryName;
}
if (isRootPath) {
rootPath = entryName;
isRootPath = false;
}
if (entry.isDirectory()) {
FileUtil.mkdirByFile(entryName);
} else if (entry.isFile()) {
FileUtil.stream2file(archiveInput, entryName, false);
}
}
} catch (Exception e) {
LOGGER.error(Symbol.BLANK, e);
}
return rootPath;
}
use of org.apache.commons.compress.archivers.tar.TarArchiveEntry in project docker-maven-plugin by fabric8io.
the class ImageArchiveUtilTest method readInvalidJsonInArchive.
@Test
public void readInvalidJsonInArchive() throws IOException {
byte[] archiveBytes;
try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
TarArchiveOutputStream tarOutput = new TarArchiveOutputStream(baos)) {
final byte[] entryData = ("}" + UUID.randomUUID().toString() + "{").getBytes();
TarArchiveEntry tarEntry = new TarArchiveEntry("not-the-" + ImageArchiveUtil.MANIFEST_JSON);
tarEntry.setSize(entryData.length);
tarOutput.putArchiveEntry(tarEntry);
tarOutput.write(entryData);
tarOutput.closeArchiveEntry();
tarOutput.finish();
archiveBytes = baos.toByteArray();
}
ImageArchiveManifest manifest = ImageArchiveUtil.readManifest(new ByteArrayInputStream(archiveBytes));
Assert.assertNull(manifest);
}
use of org.apache.commons.compress.archivers.tar.TarArchiveEntry in project karaf by apache.
the class ArchiveMojo method addFileToTarGz.
private void addFileToTarGz(TarArchiveOutputStream tOut, Path f, String base) throws IOException {
if (Files.isDirectory(f)) {
String entryName = base + f.getFileName().toString() + "/";
TarArchiveEntry tarEntry = new TarArchiveEntry(entryName);
tOut.putArchiveEntry(tarEntry);
tOut.closeArchiveEntry();
try (DirectoryStream<Path> children = Files.newDirectoryStream(f)) {
for (Path child : children) {
addFileToTarGz(tOut, child, entryName);
}
}
} else if (useSymLinks && Files.isSymbolicLink(f)) {
String entryName = base + f.getFileName().toString();
TarArchiveEntry tarEntry = new TarArchiveEntry(entryName, TarConstants.LF_SYMLINK);
tarEntry.setLinkName(Files.readSymbolicLink(f).toString());
tOut.putArchiveEntry(tarEntry);
tOut.closeArchiveEntry();
} else {
String entryName = base + f.getFileName().toString();
TarArchiveEntry tarEntry = new TarArchiveEntry(entryName);
tarEntry.setSize(Files.size(f));
if (entryName.contains("/bin/") || (!usePathPrefix && entryName.startsWith("bin/"))) {
if (entryName.endsWith(".bat")) {
tarEntry.setMode(0644);
} else {
tarEntry.setMode(0755);
}
}
tOut.putArchiveEntry(tarEntry);
Files.copy(f, tOut);
tOut.closeArchiveEntry();
}
}
use of org.apache.commons.compress.archivers.tar.TarArchiveEntry in project twister2 by DSC-SPIDAL.
the class TarGzipPacker method addZipToArchive.
/**
* given tar.gz file will be copied to this tar.gz file.
* all files will be transferred to new tar.gz file one by one.
* original directory structure will be kept intact
*
* @param zipFile the archive file to be copied to the new archive
* @param dirPrefixForTar sub path inside the archive
*/
public boolean addZipToArchive(String zipFile, String dirPrefixForTar) {
try {
// construct input stream
ZipFile zipFileObj = new ZipFile(zipFile);
Enumeration<? extends ZipEntry> entries = zipFileObj.entries();
// copy the existing entries from source gzip file
while (entries.hasMoreElements()) {
ZipEntry nextEntry = entries.nextElement();
TarArchiveEntry entry = new TarArchiveEntry(dirPrefixForTar + nextEntry.getName());
entry.setSize(nextEntry.getSize());
entry.setModTime(nextEntry.getTime());
tarOutputStream.putArchiveEntry(entry);
IOUtils.copy(zipFileObj.getInputStream(nextEntry), tarOutputStream);
tarOutputStream.closeArchiveEntry();
}
zipFileObj.close();
return true;
} catch (IOException ioe) {
LOG.log(Level.SEVERE, "Archive File can not be added: " + zipFile, ioe);
return false;
}
}
use of org.apache.commons.compress.archivers.tar.TarArchiveEntry in project twister2 by DSC-SPIDAL.
the class TarGzipPacker method addFileToArchive.
/**
* add one file to tar.gz file
*
* @param file file to be added to the tar.gz
*/
public boolean addFileToArchive(File file, String dirPrefixForTar) {
try {
String filePathInTar = dirPrefixForTar + file.getName();
TarArchiveEntry entry = new TarArchiveEntry(file, filePathInTar);
entry.setSize(file.length());
if (!file.isDirectory() && file.canExecute()) {
entry.setMode(0755);
}
tarOutputStream.putArchiveEntry(entry);
IOUtils.copy(new FileInputStream(file), tarOutputStream);
tarOutputStream.closeArchiveEntry();
return true;
} catch (IOException e) {
LOG.log(Level.SEVERE, "File can not be added: " + file.getName(), e);
return false;
}
}
Aggregations