Search in sources :

Example 81 with TarArchiveOutputStream

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();
}
Also used : ZipArchiveOutputStream(org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream) ZipArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry)

Example 82 with TarArchiveOutputStream

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;
}
Also used : GZIPOutputStream(java.util.zip.GZIPOutputStream) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream) ArchiveOutputStream(org.apache.commons.compress.archivers.ArchiveOutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) ZipArchiveOutputStream(org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream) ZipArchiveOutputStream(org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream) BackupStrategy(jetbrains.exodus.backup.BackupStrategy) VirtualFileDescriptor(jetbrains.exodus.backup.VirtualFileDescriptor) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream) ArchiveOutputStream(org.apache.commons.compress.archivers.ArchiveOutputStream) ZipArchiveOutputStream(org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream) NotNull(org.jetbrains.annotations.NotNull)

Example 83 with TarArchiveOutputStream

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");
    }
}
Also used : GZIPOutputStream(java.util.zip.GZIPOutputStream) FileOutputStream(java.io.FileOutputStream) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream) IOException(java.io.IOException) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) BackupStrategy(jetbrains.exodus.backup.BackupStrategy)

Example 84 with TarArchiveOutputStream

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();
    }
}
Also used : GzipCompressorOutputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream) FileOutputStream(java.io.FileOutputStream) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry)

Example 85 with TarArchiveOutputStream

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;
}
Also used : GzipCompressorOutputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream)

Aggregations

TarArchiveOutputStream (org.apache.commons.compress.archivers.tar.TarArchiveOutputStream)101 File (java.io.File)42 TarArchiveEntry (org.apache.commons.compress.archivers.tar.TarArchiveEntry)42 FileOutputStream (java.io.FileOutputStream)37 GzipCompressorOutputStream (org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream)30 BufferedOutputStream (java.io.BufferedOutputStream)22 IOException (java.io.IOException)21 ByteArrayOutputStream (java.io.ByteArrayOutputStream)20 FileInputStream (java.io.FileInputStream)19 Path (java.nio.file.Path)17 GZIPOutputStream (java.util.zip.GZIPOutputStream)17 ByteArrayInputStream (java.io.ByteArrayInputStream)15 OutputStream (java.io.OutputStream)13 Test (org.junit.Test)12 ArchiveEntry (org.apache.commons.compress.archivers.ArchiveEntry)11 ArchiveOutputStream (org.apache.commons.compress.archivers.ArchiveOutputStream)11 TarArchiveInputStream (org.apache.commons.compress.archivers.tar.TarArchiveInputStream)9 ZipArchiveOutputStream (org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream)9 ImageArchiveManifest (io.fabric8.maven.docker.model.ImageArchiveManifest)8 InputStream (java.io.InputStream)7