Example usage for org.apache.commons.compress.archivers ArchiveEntry getSize

List of usage examples for org.apache.commons.compress.archivers ArchiveEntry getSize

Introduction

In this page you can find the example usage for org.apache.commons.compress.archivers ArchiveEntry getSize.

Prototype

public long getSize();

Source Link

Document

The (uncompressed) size of the entry.

Usage

From source file:io.github.retz.executor.FileManager.java

private static boolean needsDecompression(File file, String dir) throws IOException, ArchiveException {
    // To use autodetect feature of ArchiveStreamFactory, input stream must be wrapped
    // with BufferedInputStream. From commons-compress mailing list.
    LOG.debug("loading file .... {} as {}", file, file.getPath());

    boolean needsDecompression = false;
    try (ArchiveInputStream ain = createAIS(file)) {
        while (ain != null) {
            ArchiveEntry entry = ain.getNextEntry();
            if (entry == null)
                break;

            // LOG.debug("name: {} size:{} dir: {}", entry.getName(), entry.getSize(), entry.isDirectory());
            File f = new File(FilenameUtils.concat(dir, entry.getName()));
            // LOG.debug("{} exists: {}, size:{}, dir:{}", f, f.exists(), f.length(), f.isDirectory());
            if (f.isDirectory()) {
                continue;
            } else if (!f.exists() || entry.getSize() != f.length()) {
                LOG.info("File {} differs: seems not decompressed before", f);
                needsDecompression = true;
                break;
            }/*from   w w  w  .  j ava 2  s  . co m*/
        }
    } catch (ArchiveException e) {
        needsDecompression = true;
    }
    return needsDecompression;
}

From source file:com.glaf.core.util.ZipUtils.java

/**
 * /*from w w w  . java 2  s.  c  o  m*/
 * zip
 * 
 * @param zipFilePath
 *            zip,  "/var/data/aa.zip"
 * 
 * @param saveFileDir
 *            ?, "/var/test/"
 */
public static void decompressZip(String zipFilePath, String saveFileDir) {
    if (isEndsWithZip(zipFilePath)) {
        File file = new File(zipFilePath);
        if (file.exists() && file.isFile()) {
            InputStream inputStream = null;
            ZipArchiveInputStream zais = null;
            try {
                inputStream = new FileInputStream(file);
                zais = new ZipArchiveInputStream(inputStream);
                ArchiveEntry archiveEntry = null;
                while ((archiveEntry = zais.getNextEntry()) != null) {
                    String entryFileName = archiveEntry.getName();
                    String entryFilePath = saveFileDir + entryFileName;
                    byte[] content = new byte[(int) archiveEntry.getSize()];
                    zais.read(content);
                    OutputStream os = null;
                    try {
                        File entryFile = new File(entryFilePath);
                        os = new BufferedOutputStream(new FileOutputStream(entryFile));
                        os.write(content);
                    } catch (IOException e) {
                        throw new IOException(e);
                    } finally {
                        IOUtils.closeStream(os);
                    }
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            } finally {
                IOUtils.closeStream(zais);
                IOUtils.closeStream(inputStream);
            }
        }
    }
}

From source file:io.sloeber.core.managers.InternalPackageManager.java

public static IStatus extract(ArchiveInputStream in, File destFolder, int stripPath, boolean overwrite,
        IProgressMonitor pMonitor) throws IOException, InterruptedException {

    // Folders timestamps must be set at the end of archive extraction
    // (because creating a file in a folder alters the folder's timestamp)
    Map<File, Long> foldersTimestamps = new HashMap<>();

    String pathPrefix = new String();

    Map<File, File> hardLinks = new HashMap<>();
    Map<File, Integer> hardLinksMode = new HashMap<>();
    Map<File, String> symLinks = new HashMap<>();
    Map<File, Long> symLinksModifiedTimes = new HashMap<>();

    // Cycle through all the archive entries
    while (true) {
        ArchiveEntry entry = in.getNextEntry();
        if (entry == null) {
            break;
        }//from   w  ww  .  jav  a 2 s . co  m

        // Extract entry info
        long size = entry.getSize();
        String name = entry.getName();
        boolean isDirectory = entry.isDirectory();
        boolean isLink = false;
        boolean isSymLink = false;
        String linkName = null;
        Integer mode = null;
        Long modifiedTime = new Long(entry.getLastModifiedDate().getTime());

        pMonitor.subTask("Processing " + name); //$NON-NLS-1$

        {
            // Skip MacOSX metadata
            // http://superuser.com/questions/61185/why-do-i-get-files-like-foo-in-my-tarball-on-os-x
            int slash = name.lastIndexOf('/');
            if (slash == -1) {
                if (name.startsWith("._")) { //$NON-NLS-1$
                    continue;
                }
            } else {
                if (name.substring(slash + 1).startsWith("._")) { //$NON-NLS-1$
                    continue;
                }
            }
        }

        // Skip git metadata
        // http://www.unix.com/unix-for-dummies-questions-and-answers/124958-file-pax_global_header-means-what.html
        if (name.contains("pax_global_header")) { //$NON-NLS-1$
            continue;
        }

        if (entry instanceof TarArchiveEntry) {
            TarArchiveEntry tarEntry = (TarArchiveEntry) entry;
            mode = new Integer(tarEntry.getMode());
            isLink = tarEntry.isLink();
            isSymLink = tarEntry.isSymbolicLink();
            linkName = tarEntry.getLinkName();
        }

        // On the first archive entry, if requested, detect the common path
        // prefix to be stripped from filenames
        int localstripPath = stripPath;
        if (localstripPath > 0 && pathPrefix.isEmpty()) {
            int slash = 0;
            while (localstripPath > 0) {
                slash = name.indexOf("/", slash); //$NON-NLS-1$
                if (slash == -1) {
                    throw new IOException(Messages.Manager_archiver_eror_single_root_folder_required);
                }
                slash++;
                localstripPath--;
            }
            pathPrefix = name.substring(0, slash);
        }

        // Strip the common path prefix when requested
        if (!name.startsWith(pathPrefix)) {
            throw new IOException(Messages.Manager_archive_error_root_folder_name_mismatch.replace(FILE, name)
                    .replace(FOLDER, pathPrefix));

        }
        name = name.substring(pathPrefix.length());
        if (name.isEmpty()) {
            continue;
        }
        File outputFile = new File(destFolder, name);

        File outputLinkedFile = null;
        if (isLink && linkName != null) {
            if (!linkName.startsWith(pathPrefix)) {
                throw new IOException(Messages.Manager_archive_error_root_folder_name_mismatch
                        .replace(FILE, name).replace(FOLDER, pathPrefix));
            }
            linkName = linkName.substring(pathPrefix.length());
            outputLinkedFile = new File(destFolder, linkName);
        }
        if (isSymLink) {
            // Symbolic links are referenced with relative paths
            outputLinkedFile = new File(linkName);
            if (outputLinkedFile.isAbsolute()) {
                System.err.println(Messages.Manager_archive_error_symbolic_link_to_absolute_path
                        .replace(FILE, outputFile.toString()).replace(FOLDER, outputLinkedFile.toString()));
                System.err.println();
            }
        }

        // Safety check
        if (isDirectory) {
            if (outputFile.isFile() && !overwrite) {
                throw new IOException(
                        Messages.Manager_Cant_create_folder_exists.replace(FILE, outputFile.getPath()));
            }
        } else {
            // - isLink
            // - isSymLink
            // - anything else
            if (outputFile.exists() && !overwrite) {
                throw new IOException(
                        Messages.Manager_Cant_extract_file_exist.replace(FILE, outputFile.getPath()));
            }
        }

        // Extract the entry
        if (isDirectory) {
            if (!outputFile.exists() && !outputFile.mkdirs()) {
                throw new IOException(Messages.Manager_Cant_create_folder.replace(FILE, outputFile.getPath()));
            }
            foldersTimestamps.put(outputFile, modifiedTime);
        } else if (isLink) {
            hardLinks.put(outputFile, outputLinkedFile);
            hardLinksMode.put(outputFile, mode);
        } else if (isSymLink) {
            symLinks.put(outputFile, linkName);
            symLinksModifiedTimes.put(outputFile, modifiedTime);
        } else {
            // Create the containing folder if not exists
            if (!outputFile.getParentFile().isDirectory()) {
                outputFile.getParentFile().mkdirs();
            }
            copyStreamToFile(in, size, outputFile);
            outputFile.setLastModified(modifiedTime.longValue());
        }

        // Set file/folder permission
        if (mode != null && !isSymLink && outputFile.exists()) {
            chmod(outputFile, mode.intValue());
        }
    }

    for (Map.Entry<File, File> entry : hardLinks.entrySet()) {
        if (entry.getKey().exists() && overwrite) {
            entry.getKey().delete();
        }
        link(entry.getValue(), entry.getKey());
        Integer mode = hardLinksMode.get(entry.getKey());
        if (mode != null) {
            chmod(entry.getKey(), mode.intValue());
        }
    }

    for (Map.Entry<File, String> entry : symLinks.entrySet()) {
        if (entry.getKey().exists() && overwrite) {
            entry.getKey().delete();
        }

        symlink(entry.getValue(), entry.getKey());
        entry.getKey().setLastModified(symLinksModifiedTimes.get(entry.getKey()).longValue());
    }

    // Set folders timestamps
    for (Map.Entry<File, Long> entry : foldersTimestamps.entrySet()) {
        entry.getKey().setLastModified(entry.getValue().longValue());
    }

    return Status.OK_STATUS;

}

From source file:io.sloeber.core.managers.Manager.java

public static IStatus extract(ArchiveInputStream in, File destFolder, int stripPath, boolean overwrite,
        IProgressMonitor pMonitor) throws IOException, InterruptedException {

    // Folders timestamps must be set at the end of archive extraction
    // (because creating a file in a folder alters the folder's timestamp)
    Map<File, Long> foldersTimestamps = new HashMap<>();

    String pathPrefix = ""; //$NON-NLS-1$

    Map<File, File> hardLinks = new HashMap<>();
    Map<File, Integer> hardLinksMode = new HashMap<>();
    Map<File, String> symLinks = new HashMap<>();
    Map<File, Long> symLinksModifiedTimes = new HashMap<>();

    // Cycle through all the archive entries
    while (true) {
        ArchiveEntry entry = in.getNextEntry();
        if (entry == null) {
            break;
        }//from  ww  w. ja va2s.c o  m

        // Extract entry info
        long size = entry.getSize();
        String name = entry.getName();
        boolean isDirectory = entry.isDirectory();
        boolean isLink = false;
        boolean isSymLink = false;
        String linkName = null;
        Integer mode = null;
        Long modifiedTime = new Long(entry.getLastModifiedDate().getTime());

        pMonitor.subTask("Processing " + name); //$NON-NLS-1$

        {
            // Skip MacOSX metadata
            // http://superuser.com/questions/61185/why-do-i-get-files-like-foo-in-my-tarball-on-os-x
            int slash = name.lastIndexOf('/');
            if (slash == -1) {
                if (name.startsWith("._")) { //$NON-NLS-1$
                    continue;
                }
            } else {
                if (name.substring(slash + 1).startsWith("._")) { //$NON-NLS-1$
                    continue;
                }
            }
        }

        // Skip git metadata
        // http://www.unix.com/unix-for-dummies-questions-and-answers/124958-file-pax_global_header-means-what.html
        if (name.contains("pax_global_header")) { //$NON-NLS-1$
            continue;
        }

        if (entry instanceof TarArchiveEntry) {
            TarArchiveEntry tarEntry = (TarArchiveEntry) entry;
            mode = new Integer(tarEntry.getMode());
            isLink = tarEntry.isLink();
            isSymLink = tarEntry.isSymbolicLink();
            linkName = tarEntry.getLinkName();
        }

        // On the first archive entry, if requested, detect the common path
        // prefix to be stripped from filenames
        int localstripPath = stripPath;
        if (localstripPath > 0 && pathPrefix.isEmpty()) {
            int slash = 0;
            while (localstripPath > 0) {
                slash = name.indexOf("/", slash); //$NON-NLS-1$
                if (slash == -1) {
                    throw new IOException(Messages.Manager_no_single_root_folder);
                }
                slash++;
                localstripPath--;
            }
            pathPrefix = name.substring(0, slash);
        }

        // Strip the common path prefix when requested
        if (!name.startsWith(pathPrefix)) {
            throw new IOException(Messages.Manager_no_single_root_folder_while_file + name
                    + Messages.Manager_is_outside + pathPrefix);
        }
        name = name.substring(pathPrefix.length());
        if (name.isEmpty()) {
            continue;
        }
        File outputFile = new File(destFolder, name);

        File outputLinkedFile = null;
        if (isLink && linkName != null) {
            if (!linkName.startsWith(pathPrefix)) {
                throw new IOException(Messages.Manager_no_single_root_folder_while_file + linkName
                        + Messages.Manager_is_outside + pathPrefix);
            }
            linkName = linkName.substring(pathPrefix.length());
            outputLinkedFile = new File(destFolder, linkName);
        }
        if (isSymLink) {
            // Symbolic links are referenced with relative paths
            outputLinkedFile = new File(linkName);
            if (outputLinkedFile.isAbsolute()) {
                System.err.println(Messages.Manager_Warning_file + outputFile
                        + Messages.Manager_links_to_absolute_path + outputLinkedFile);
                System.err.println();
            }
        }

        // Safety check
        if (isDirectory) {
            if (outputFile.isFile() && !overwrite) {
                throw new IOException(
                        Messages.Manager_Cant_create_folder + outputFile + Messages.Manager_File_exists);
            }
        } else {
            // - isLink
            // - isSymLink
            // - anything else
            if (outputFile.exists() && !overwrite) {
                throw new IOException(
                        Messages.Manager_Cant_extract_file + outputFile + Messages.Manager_File_already_exists);
            }
        }

        // Extract the entry
        if (isDirectory) {
            if (!outputFile.exists() && !outputFile.mkdirs()) {
                throw new IOException(Messages.Manager_Cant_create_folder + outputFile);
            }
            foldersTimestamps.put(outputFile, modifiedTime);
        } else if (isLink) {
            hardLinks.put(outputFile, outputLinkedFile);
            hardLinksMode.put(outputFile, mode);
        } else if (isSymLink) {
            symLinks.put(outputFile, linkName);
            symLinksModifiedTimes.put(outputFile, modifiedTime);
        } else {
            // Create the containing folder if not exists
            if (!outputFile.getParentFile().isDirectory()) {
                outputFile.getParentFile().mkdirs();
            }
            copyStreamToFile(in, size, outputFile);
            outputFile.setLastModified(modifiedTime.longValue());
        }

        // Set file/folder permission
        if (mode != null && !isSymLink && outputFile.exists()) {
            chmod(outputFile, mode.intValue());
        }
    }

    for (Map.Entry<File, File> entry : hardLinks.entrySet()) {
        if (entry.getKey().exists() && overwrite) {
            entry.getKey().delete();
        }
        link(entry.getValue(), entry.getKey());
        Integer mode = hardLinksMode.get(entry.getKey());
        if (mode != null) {
            chmod(entry.getKey(), mode.intValue());
        }
    }

    for (Map.Entry<File, String> entry : symLinks.entrySet()) {
        if (entry.getKey().exists() && overwrite) {
            entry.getKey().delete();
        }
        symlink(entry.getValue(), entry.getKey());
        entry.getKey().setLastModified(symLinksModifiedTimes.get(entry.getKey()).longValue());
    }

    // Set folders timestamps
    for (Map.Entry<File, Long> entry : foldersTimestamps.entrySet()) {
        entry.getKey().setLastModified(entry.getValue().longValue());
    }

    return Status.OK_STATUS;

}

From source file:jetbrains.exodus.util.CompressBackupUtilTest.java

@Test
public void testFileArchived() throws Exception {
    File src = new File(randName + ".txt");
    FileWriter fw = new FileWriter(src);
    fw.write("12345");
    fw.close();/* ww w.ja v  a  2 s  . co m*/
    CompressBackupUtil.tar(src, dest);
    Assert.assertTrue("No destination archive created", dest.exists());
    TarArchiveInputStream tai = new TarArchiveInputStream(
            new GZIPInputStream(new BufferedInputStream(new FileInputStream(dest))));
    ArchiveEntry entry = tai.getNextEntry();
    Assert.assertNotNull("No entry found in destination archive", entry);
    Assert.assertEquals("Entry has wrong size", 5, entry.getSize());
}

From source file:de.dentrassi.eclipse.rpm.editor.EditorImpl.java

private RpmInformation load(final InputStream stream) {
    try (RpmInputStream in = new RpmInputStream(stream)) {
        final RpmLead lead = in.getLead();

        final InputHeader<RpmTag> header = in.getPayloadHeader();
        final InputHeader<RpmSignatureTag> sigHeader = in.getSignatureHeader();

        final CpioArchiveInputStream cpio = in.getCpioStream();

        ArchiveEntry entry;

        final List<FileEntry> files = new ArrayList<>();
        while ((entry = cpio.getNextEntry()) != null) {
            final FileEntry fe = new FileEntry(entry.getName(), entry.getSize(),
                    entry.getLastModifiedDate().toInstant());
            files.add(fe);//from   w ww . ja  v a  2  s.c o m
        }

        return new RpmInformation(lead, header, sigHeader, files);
    } catch (final IOException e) {
        return null;
    }
}

From source file:com.softenido.cafedark.io.virtual.ZipVirtualFileSystem.java

public ZipVirtualFileSystem(String[] items, String path, ArchiveEntry entry) {
    this.items = items;
    this.name = items[items.length - 1];
    this.path = path;
    if (entry != null) {
        this.length = entry.getSize();
        this.directory = entry.isDirectory();
    } else {/*from  w  w w .j  ava2  s.c  om*/
        this.length = 0;
        this.directory = false;
    }
}

From source file:de.flapdoodle.embed.process.extract.AbstractExtractor.java

@Override
public ExtractedFileSet extract(DownloadConfig runtime, File source, FilesToExtract toExtract)
        throws IOException {
    Builder builder = ExtractedFileSet.builder(toExtract.baseDir())
            .baseDirIsGenerated(toExtract.baseDirIsGenerated());

    ProgressListener progressListener = runtime.getProgressListener();
    String progressLabel = "Extract " + source;
    progressListener.start(progressLabel);

    ArchiveWrapper archive = archiveStreamWithExceptionHint(source);

    try {/*  w ww  .j  av  a2s.c  om*/
        ArchiveEntry entry;
        while ((entry = archive.getNextEntry()) != null) {
            IExtractionMatch match = toExtract.find(new CommonsArchiveEntryAdapter(entry));
            if (match != null) {
                if (archive.canReadEntryData(entry)) {
                    long size = entry.getSize();
                    FileType type = match.type();
                    File file = match.write(archive.asStream(entry), size);
                    if (type == FileType.Executable) {
                        builder.executable(file);
                    } else {
                        builder.addLibraryFiles(file);
                    }
                    //                  destination.setExecutable(true);
                    progressListener.info(progressLabel, "extract " + entry.getName());
                }
                if (toExtract.nothingLeft()) {
                    progressListener.info(progressLabel, "nothing left");
                    break;
                }
            }
        }

    } finally {
        archive.close();
    }

    progressListener.done(progressLabel);

    return builder.build();
}

From source file:at.beris.virtualfile.provider.LocalArchivedFileOperationProvider.java

@Override
public Boolean exists(FileModel model) throws IOException {
    String archivePath = getArchivePath(model);
    String targetArchiveEntryPath = model.getUrl().getPath().substring(archivePath.length() + 1);

    ArchiveInputStream ais = null;//from  w w  w  .j ava2s .  com
    InputStream fis = null;

    try {
        ArchiveStreamFactory factory = new ArchiveStreamFactory();
        fis = new BufferedInputStream(new FileInputStream(new java.io.File(archivePath)));
        ais = factory.createArchiveInputStream(fis);
        ArchiveEntry archiveEntry;

        while ((archiveEntry = ais.getNextEntry()) != null) {
            String archiveEntryPath = archiveEntry.getName();

            if (archiveEntryPath.equals(targetArchiveEntryPath)) {
                model.setSize(archiveEntry.getSize());
                model.setLastModifiedTime(FileTime.fromMillis(archiveEntry.getLastModifiedDate().getTime()));

                if (model.getUrl().toString().endsWith("/") && (!archiveEntry.isDirectory())) {
                    String urlString = model.getUrl().toString();
                    model.setUrl(UrlUtils.newUrl(urlString.substring(0, urlString.length() - 1)));
                } else if (!model.getUrl().toString().endsWith("/") && (archiveEntry.isDirectory())) {
                    String urlString = model.getUrl().toString() + "/";
                    model.setUrl(UrlUtils.newUrl(urlString));
                }
                break;
            }
        }
    } catch (ArchiveException e) {
        throw new IOException(e);
    } finally {
        if (ais != null)
            ais.close();
        if (fis != null)
            fis.close();
    }
    return false;
}

From source file:jetbrains.exodus.util.CompressBackupUtilTest.java

@Test
public void testFolderArchived() throws Exception {
    File src = new File(randName);
    src.mkdir();//  w w  w  .j av  a  2  s . c  o  m
    FileWriter fw = new FileWriter(new File(src, "1.txt"));
    fw.write("12345");
    fw.close();
    fw = new FileWriter(new File(src, "2.txt"));
    fw.write("12");
    fw.close();
    CompressBackupUtil.tar(src, dest);
    Assert.assertTrue("No destination archive created", dest.exists());
    TarArchiveInputStream tai = new TarArchiveInputStream(
            new GZIPInputStream(new BufferedInputStream(new FileInputStream(dest))));
    ArchiveEntry entry1 = tai.getNextEntry();
    ArchiveEntry entry2 = tai.getNextEntry();
    if (entry1.getName().compareTo(entry2.getName()) > 0) { // kinda sort them lol
        ArchiveEntry tmp = entry1;
        entry1 = entry2;
        entry2 = tmp;
    }
    Assert.assertNotNull("No entry found in destination archive", entry1);
    Assert.assertEquals("Entry has wrong size", 5, entry1.getSize());
    System.out.println(entry1.getName());
    Assert.assertEquals("Entry has wrong relative path", src.getName() + "/1.txt", entry1.getName());
    System.out.println(entry2.getName());
    Assert.assertEquals("Entry has wrong size", 2, entry2.getSize());
    Assert.assertEquals("Entry has wrong relative path", src.getName() + "/2.txt", entry2.getName());
}