use of org.apache.commons.compress.archivers.tar.TarArchiveOutputStream in project xodus by JetBrains.
the class CompressBackupUtil method archiveFile.
/**
* Adds the file to the tar archive represented by output stream. It's caller's responsibility to close output stream
* properly.
*
* @param out target archive.
* @param source file to be added.
* @param fileSize size of the file (which is known in most cases).
* @throws IOException in case of any issues with underlying store.
*/
public static void archiveFile(@NotNull final ArchiveOutputStream out, @NotNull final VirtualFileDescriptor source, final long fileSize) throws IOException {
if (!source.hasContent()) {
throw new IllegalArgumentException("Provided source is not a file: " + source.getPath());
}
// noinspection ChainOfInstanceofChecks
if (out instanceof TarArchiveOutputStream) {
final TarArchiveEntry entry = new TarArchiveEntry(source.getPath() + source.getName());
entry.setSize(fileSize);
entry.setModTime(source.getTimeStamp());
out.putArchiveEntry(entry);
} else if (out instanceof ZipArchiveOutputStream) {
final ZipArchiveEntry entry = new ZipArchiveEntry(source.getPath() + source.getName());
entry.setSize(fileSize);
entry.setTime(source.getTimeStamp());
out.putArchiveEntry(entry);
} else {
throw new IOException("Unknown archive output stream");
}
final InputStream input = source.getInputStream();
try {
IOUtil.copyStreams(input, fileSize, out, IOUtil.getBUFFER_ALLOCATOR());
} finally {
if (source.shouldCloseStream()) {
input.close();
}
}
out.closeArchiveEntry();
}
use of org.apache.commons.compress.archivers.tar.TarArchiveOutputStream in project xodus by JetBrains.
the class CompressBackupUtil method backup.
/**
* For specified {@linkplain Backupable} {@code source} and {@code target} backup file, does backup.
* Typically, {@code source} is an {@linkplain Environment} or an {@linkplain PersistentEntityStore}
* instance. Set {@code zip = true} to create {@code .zip} backup file, otherwise {@code .tar.gz} file will be created.
*
* <p>{@linkplain Environment} and {@linkplain PersistentEntityStore} instances don't require any specific actions
* (like, e.g., switching to read-only mode) to do backups and get consistent copies of data within backups files.
* So backup can be performed on-the-fly not affecting database operations.
*
* @param source an instance of {@linkplain Backupable}
* @param target target backup file (either .zip or .tag.gz)
* @param zip {@code true} to create {@code .zip} backup file, rather than {@code .tar.gz} one
* @return backup file the same as specified {@code target}
* @throws Exception something went wrong
*/
@NotNull
public static File backup(@NotNull final Backupable source, @NotNull final File target, final boolean zip) throws Exception {
if (target.exists()) {
throw new IOException("Backup file already exists:" + target.getAbsolutePath());
}
final BackupStrategy strategy = source.getBackupStrategy();
strategy.beforeBackup();
try (OutputStream output = new BufferedOutputStream(new FileOutputStream(target))) {
final ArchiveOutputStream archive;
if (zip) {
final ZipArchiveOutputStream zipArchive = new ZipArchiveOutputStream(output);
zipArchive.setLevel(Deflater.BEST_COMPRESSION);
archive = zipArchive;
} else {
archive = new TarArchiveOutputStream(new GZIPOutputStream(output));
}
try (ArchiveOutputStream aos = archive) {
for (final VirtualFileDescriptor fd : strategy.getContents()) {
if (strategy.isInterrupted()) {
break;
}
if (fd.hasContent()) {
final long fileSize = Math.min(fd.getFileSize(), strategy.acceptFile(fd));
if (fileSize > 0L) {
archiveFile(aos, fd, fileSize);
}
}
}
}
if (strategy.isInterrupted()) {
logger.info("Backup interrupted, deleting \"" + target.getName() + "\"...");
IOUtil.deleteFile(target);
} else {
logger.info("Backup file \"" + target.getName() + "\" created.");
}
} catch (Throwable t) {
strategy.onError(t);
throw ExodusException.toExodusException(t, "Backup failed");
} finally {
strategy.afterBackup();
}
return target;
}
use of org.apache.commons.compress.archivers.tar.TarArchiveOutputStream in project xodus by JetBrains.
the class EnvironmentTestsBase method archiveDB.
public static void archiveDB(final String location, final String target) {
try {
System.out.println("Dumping " + location + " to " + target);
final File root = new File(location);
final File targetFile = new File(target);
TarArchiveOutputStream tarGz = new TarArchiveOutputStream(new GZIPOutputStream(new BufferedOutputStream(new FileOutputStream(targetFile)), 0x1000));
for (final File file : IOUtil.listFiles(root)) {
final long fileSize = file.length();
if (file.isFile() && fileSize != 0) {
CompressBackupUtil.archiveFile(tarGz, new BackupStrategy.FileDescriptor(file, ""), fileSize);
}
}
tarGz.close();
} catch (IOException ioe) {
System.out.println("Can't create backup");
}
}
use of org.apache.commons.compress.archivers.tar.TarArchiveOutputStream in project nodejs-plugin by jenkinsci.
the class NodeJSInstallerTest method fillArchive.
private void fillArchive(File file, String fileEntry, byte[] content) throws IOException {
try (TarArchiveOutputStream zf = new TarArchiveOutputStream(new GzipCompressorOutputStream(new FileOutputStream(file)))) {
TarArchiveEntry ze = new TarArchiveEntry(fileEntry);
ze.setSize(content.length);
zf.putArchiveEntry(ze);
IOUtils.write(content, zf);
zf.closeArchiveEntry();
}
}
use of org.apache.commons.compress.archivers.tar.TarArchiveOutputStream in project RecordManager2 by moravianlibrary.
the class TarGzUtils method compress.
public static File compress(String sourcePath, String targetPath) throws IOException {
FileOutputStream fOut = null;
BufferedOutputStream bOut = null;
GzipCompressorOutputStream gzOut = null;
TarArchiveOutputStream tOut = null;
File targetFile = new File(targetPath);
try {
fOut = new FileOutputStream(targetFile);
bOut = new BufferedOutputStream(fOut);
gzOut = new GzipCompressorOutputStream(bOut);
tOut = new TarArchiveOutputStream(gzOut);
tOut.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
addFileToTarGz(tOut, sourcePath, "");
} finally {
if (tOut != null) {
tOut.finish();
tOut.close();
}
if (gzOut != null)
gzOut.close();
if (bOut != null)
bOut.close();
if (fOut != null)
fOut.close();
}
return targetFile;
}
Aggregations