本文整理汇总了Java中org.apache.commons.compress.archivers.tar.TarArchiveOutputStream.putArchiveEntry方法的典型用法代码示例。如果您正苦于以下问题:Java TarArchiveOutputStream.putArchiveEntry方法的具体用法?Java TarArchiveOutputStream.putArchiveEntry怎么用?Java TarArchiveOutputStream.putArchiveEntry使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.commons.compress.archivers.tar.TarArchiveOutputStream
的用法示例。
在下文中一共展示了TarArchiveOutputStream.putArchiveEntry方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: addFileToTarGz
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream; //导入方法依赖的package包/类
/**
* Recursively adds a directory to a .tar.gz. Adapted from http://stackoverflow.com/questions/13461393/compress-directory-to-tar-gz-with-commons-compress
*
* @param tOut The .tar.gz to add the directory to
* @param path The location of the folders and files to add
* @param base The base path of entry in the .tar.gz
* @throws IOException Any exceptions thrown during tar creation
*/
private void addFileToTarGz(TarArchiveOutputStream tOut, String path, String base) throws IOException {
File f = new File(path);
String entryName = base + f.getName();
TarArchiveEntry tarEntry = new TarArchiveEntry(f, entryName);
tOut.putArchiveEntry(tarEntry);
Platform.runLater(() -> fileLabel.setText("Processing " + f.getPath()));
if (f.isFile()) {
FileInputStream fin = new FileInputStream(f);
IOUtils.copy(fin, tOut);
fin.close();
tOut.closeArchiveEntry();
} else {
tOut.closeArchiveEntry();
File[] children = f.listFiles();
if (children != null) {
for (File child : children) {
addFileToTarGz(tOut, child.getAbsolutePath(), entryName + "/");
}
}
}
}
示例2: addControlContent
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream; //导入方法依赖的package包/类
private void addControlContent ( final TarArchiveOutputStream out, final String name, final ContentProvider content, final int mode ) throws IOException
{
if ( content == null || !content.hasContent () )
{
return;
}
final TarArchiveEntry entry = new TarArchiveEntry ( name );
if ( mode >= 0 )
{
entry.setMode ( mode );
}
entry.setUserName ( "root" );
entry.setGroupName ( "root" );
entry.setSize ( content.getSize () );
entry.setModTime ( this.getTimestampProvider ().getModTime () );
out.putArchiveEntry ( entry );
try ( InputStream stream = content.createInputStream () )
{
ByteStreams.copy ( stream, out );
}
out.closeArchiveEntry ();
}
示例3: addFileToTarGz
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream; //导入方法依赖的package包/类
/**
* This function copies a given file to the zip file
*
* @param tarArchiveOutputStream tar output stream of the zip file
* @param file the file to insert to the zar file
*
* @throws IOException
*/
private static void addFileToTarGz(TarArchiveOutputStream tarArchiveOutputStream, File file)
throws IOException
{
String entryName = file.getName();
TarArchiveEntry tarEntry = new TarArchiveEntry(file, entryName);
tarArchiveOutputStream.putArchiveEntry(tarEntry);
if (file.isFile()) {
try (FileInputStream input = new FileInputStream(file))
{
IOUtils.copy(input, tarArchiveOutputStream);
}
tarArchiveOutputStream.closeArchiveEntry();
} else {//Directory
System.out.println("The directory which need to be packed to tar folder cannot contain other directories");
}
}
示例4: archiveDir
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream; //导入方法依赖的package包/类
/**
* 目录归档
*
* @param dir
* @param taos
* TarArchiveOutputStream
* @param basePath
* @throws Exception
*/
private static void archiveDir(File dir, TarArchiveOutputStream taos,
String basePath) throws Exception {
File[] files = dir.listFiles();
if (files.length < 1) {
TarArchiveEntry entry = new TarArchiveEntry(basePath
+ dir.getName() + PATH);
taos.putArchiveEntry(entry);
taos.closeArchiveEntry();
}
for (File file : files) {
// 递归归档
archive(file, taos, basePath + dir.getName() + PATH);
}
}
示例5: addFileToTar
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream; //导入方法依赖的package包/类
private void addFileToTar(TarArchiveOutputStream tarArchiveOutputStream, String path, String base) {
File file = new File(path);
String fileName = file.getName();
if (Arrays.stream(omitPaths).anyMatch(s -> s.equals(fileName))) {
return;
}
String tarEntryName = String.join("/", base, fileName);
try {
if (file.isFile()) {
TarArchiveEntry tarEntry = new TarArchiveEntry(file, tarEntryName);
tarArchiveOutputStream.putArchiveEntry(tarEntry);
IOUtils.copy(new FileInputStream(file), tarArchiveOutputStream);
tarArchiveOutputStream.closeArchiveEntry();
} else if (file.isDirectory()) {
Arrays.stream(file.listFiles())
.filter(Objects::nonNull)
.forEach(f -> addFileToTar(tarArchiveOutputStream, f.getAbsolutePath(), tarEntryName));
} else {
log.warn("Unknown file type: " + file + " - skipping addition to tar archive");
}
} catch (IOException e) {
throw new HalException(Problem.Severity.FATAL, "Unable to file " + file.getName() + " to archive entry: " + tarEntryName + " " + e.getMessage(), e);
}
}
示例6: createArchive
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream; //导入方法依赖的package包/类
/**
* Create a gzipped tar archive containing the ProGuard/Native mapping files
*
* @param files array of mapping.txt files
* @return the tar-gzipped archive
*/
private static File createArchive(List<File> files, String uuid) {
try {
File tarZippedFile = File.createTempFile("tar-zipped-file", ".tgz");
TarArchiveOutputStream taos = new TarArchiveOutputStream(
new GZIPOutputStream(
new BufferedOutputStream(
new FileOutputStream(tarZippedFile))));
for (File file : files) {
taos.putArchiveEntry(new TarArchiveEntry(file,
(uuid != null && !uuid.isEmpty() ? uuid : UUID.randomUUID()) + ".txt"));
IOUtils.copy(new FileInputStream(file), taos);
taos.closeArchiveEntry();
}
taos.finish();
taos.close();
return tarZippedFile;
} catch (IOException e) {
failWithError("IO Exception while trying to tar and zip the file.", e);
return null;
}
}
示例7: addToTar
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream; //导入方法依赖的package包/类
private void addToTar (File dir, TarArchiveOutputStream tar, String prefix) throws IOException
{
for (String file : dir.list())
{
File f = new File(dir, file);
WorkerMain.getLogger().debug(f);
if (f.isDirectory())
addToTar(f, tar, prefix + file + "/");
else
{
TarArchiveEntry entry = new TarArchiveEntry(prefix + file);
entry.setSize(f.length());
tar.putArchiveEntry(entry);
FileInputStream in = new FileInputStream(f);
byte buf[] = new byte[8192];
int read;
while ((read = in.read(buf)) > 0)
tar.write(buf, 0, read);
in.close();
tar.closeArchiveEntry();
}
}
}
示例8: addControlContent
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream; //导入方法依赖的package包/类
private void addControlContent ( final TarArchiveOutputStream out, final String name, final ContentProvider content, final int mode, final Supplier<Instant> timestampSupplier ) throws IOException
{
if ( content == null || !content.hasContent () )
{
return;
}
final TarArchiveEntry entry = new TarArchiveEntry ( name );
if ( mode >= 0 )
{
entry.setMode ( mode );
}
entry.setUserName ( "root" );
entry.setGroupName ( "root" );
entry.setSize ( content.getSize () );
entry.setModTime ( timestampSupplier.get ().toEpochMilli () );
out.putArchiveEntry ( entry );
try ( InputStream stream = content.createInputStream () )
{
IOUtils.copy ( stream, out );
}
out.closeArchiveEntry ();
}
示例9: addFileEntry
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream; //导入方法依赖的package包/类
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();
}
示例10: addFileToArchive
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream; //导入方法依赖的package包/类
private static void addFileToArchive(TarArchiveOutputStream archiveOutputStream, File file,
String base) throws IOException {
final File absoluteFile = file.getAbsoluteFile();
final String entryName = base + file.getName();
final TarArchiveEntry tarArchiveEntry = new TarArchiveEntry(file, entryName);
archiveOutputStream.putArchiveEntry(tarArchiveEntry);
if (absoluteFile.isFile()) {
Files.copy(file.toPath(), archiveOutputStream);
archiveOutputStream.closeArchiveEntry();
} else {
archiveOutputStream.closeArchiveEntry();
if (absoluteFile.listFiles() != null) {
for (File f : absoluteFile.listFiles()) {
addFileToArchive(archiveOutputStream, f, entryName + "/");
}
}
}
}
示例11: addFileToTarGz
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream; //导入方法依赖的package包/类
/**
* Creates a tar entry for the path specified with a name built from the base
* passed in and the file/directory name. If the path is a directory, a
* recursive call is made such that the full directory is added to the tar.
*
* @param tOut
* The tar file's output stream
* @param path
* The filesystem path of the file/directory being added
* @param base
* The base prefix to for the name of the tar file entry
*
* @throws IOException
* If anything goes wrong
*/
private static void addFileToTarGz(TarArchiveOutputStream tOut, String path, String base) throws IOException {
File f = new File(path);
String entryName = base + f.getName();
TarArchiveEntry tarEntry = new TarArchiveEntry(f, entryName);
tOut.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
tOut.putArchiveEntry(tarEntry);
if (f.isFile()) {
IOUtils.copy(new FileInputStream(f), tOut);
tOut.closeArchiveEntry();
} else {
tOut.closeArchiveEntry();
File[] children = f.listFiles();
if (children != null) {
for (File child : children) {
addFileToTarGz(tOut, child.getAbsolutePath(), entryName + "/");
}
}
}
}
示例12: addFileToTarGz
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream; //导入方法依赖的package包/类
/**
* Creates a tar entry for the path specified with a name built from the base
* passed in and the file/directory name. If the path is a directory, a
* recursive call is made such that the full directory is added to the tar.
* @param tOut
* The tar file's output stream
* @param path
* The filesystem path of the file/directory being added
* @param base
* The base prefix to for the name of the tar file entry
* @throws IOException
* If anything goes wrong
*/
private static void addFileToTarGz(TarArchiveOutputStream tOut, String path, String base)
throws IOException {
File f = new File(path);
String entryName = base + f.getName();
TarArchiveEntry tarEntry = new TarArchiveEntry(f, entryName);
tOut.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
tOut.putArchiveEntry(tarEntry);
if (f.isFile()) {
IOUtils.copy(new FileInputStream(f), tOut);
tOut.closeArchiveEntry();
} else {
tOut.closeArchiveEntry();
File[] children = f.listFiles();
if (children != null) {
for (File child : children) {
addFileToTarGz(tOut, child.getAbsolutePath(), entryName + "/");
}
}
}
}
示例13: compressTar
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream; //导入方法依赖的package包/类
private static void compressTar(String parent, Resource source,TarArchiveOutputStream tos, int mode) throws IOException {
if(source.isFile()) {
//TarEntry entry = (source instanceof FileResource)?new TarEntry((FileResource)source):new TarEntry(parent);
TarArchiveEntry entry = new TarArchiveEntry(parent);
entry.setName(parent);
// mode
//100777 TODO ist das so ok?
if(mode>0) entry.setMode(mode);
else if((mode=source.getMode())>0) entry.setMode(mode);
entry.setSize(source.length());
entry.setModTime(source.lastModified());
tos.putArchiveEntry(entry);
try {
IOUtil.copy(source,tos,false);
}
finally {
tos.closeArchiveEntry();
}
}
else if(source.isDirectory()) {
compressTar(parent, source.listResources(),tos,mode);
}
}
示例14: compressData
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream; //导入方法依赖的package包/类
/**
* Compress data
*
* @param fileCompressor
* FileCompressor object
* @return
* @throws Exception
*/
@Override
public byte[] compressData(FileCompressor fileCompressor) throws Exception {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
TarArchiveOutputStream aos = new TarArchiveOutputStream(baos);
try {
for (BinaryFile binaryFile : fileCompressor.getMapBinaryFile()
.values()) {
TarArchiveEntry entry = new TarArchiveEntry(
binaryFile.getDesPath());
entry.setSize(binaryFile.getActualSize());
aos.putArchiveEntry(entry);
aos.write(binaryFile.getData());
aos.closeArchiveEntry();
}
aos.flush();
aos.finish();
} catch (Exception e) {
FileCompressor.LOGGER.error("Error on compress data", e);
} finally {
aos.close();
baos.close();
}
return baos.toByteArray();
}
示例15: addDirToArchive
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream; //导入方法依赖的package包/类
/**
* adds a directory to a zip file, recursively
*
* @param tos TarArchiveOutputStream
* @param srcFile File
*/
private void addDirToArchive(TarArchiveOutputStream tos, File srcFile) {
File[] files = srcFile.listFiles();
for (File file : files) {
if (file.isDirectory()) {
addDirToArchive(tos, file);
continue;
}
try {
logger.debug("Adding file: " + file.getPath());
TarArchiveEntry entry = new TarArchiveEntry(file.getPath());
entry.setName(repository.toURI().relativize(file.toURI()).getPath());
entry.setSize(file.length());
tos.putArchiveEntry(entry);
IOUtils.copy(new FileInputStream(file), tos);
tos.closeArchiveEntry();
} catch (IOException ioe) {
logger.error("can not add to tar file. " + ioe.getMessage());
}
}
}