Search in sources :

Example 1 with ZipEntry

use of com.mucommander.commons.file.archive.zip.provider.ZipEntry in project mucommander by mucommander.

the class ZipArchiveFile method getEntryIterator.

// ////////////////////////////////////////
// AbstractROArchiveFile implementation //
// ////////////////////////////////////////
@Override
public synchronized ArchiveEntryIterator getEntryIterator() throws IOException, UnsupportedFileOperationException {
    // If the underlying AbstractFile has random read access, use our own ZipFile implementation to read entries
    if (file.isFileOperationSupported(FileOperation.RANDOM_READ_FILE)) {
        checkZipFile();
        final Iterator<ZipEntry> iterator = zipFile.getEntries();
        return new ArchiveEntryIterator() {

            @Override
            public ArchiveEntry nextEntry() throws IOException {
                ZipEntry entry;
                if (!iterator.hasNext() || (entry = iterator.next()) == null)
                    return null;
                return createArchiveEntry(entry);
            }
        };
    } else // If the underlying AbstractFile doesn't have random read access, use java.util.zip.ZipInputStream to
    // read the entries. This is much slower than the former method as the file cannot be sought through and needs
    // to be traversed.
    {
        return new JavaUtilZipEntryIterator(new ZipInputStream(file.getInputStream()));
    }
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) ZipEntry(com.mucommander.commons.file.archive.zip.provider.ZipEntry) ArchiveEntryIterator(com.mucommander.commons.file.archive.ArchiveEntryIterator)

Example 2 with ZipEntry

use of com.mucommander.commons.file.archive.zip.provider.ZipEntry in project mucommander by mucommander.

the class ZipArchiveFile method updateEntry.

@Override
public void updateEntry(ArchiveEntry entry) throws IOException, UnsupportedFileOperationException {
    ZipEntry zipEntry = (com.mucommander.commons.file.archive.zip.provider.ZipEntry) entry.getEntryObject();
    // That is the case when a file entry exists in the Zip file but has no directory entry for the parent.
    if (zipEntry != null) {
        // Entry exists physically in the zip file
        checkZipFile();
        zipEntry.setTime(entry.getDate());
        zipEntry.setUnixMode(entry.getPermissions().getIntValue());
        // Physically update the entry's attributes in the Zip file
        zipFile.updateEntry(zipEntry);
        // Declare the zip file and entries tree up-to-date
        declareZipFileUpToDate();
        declareEntriesTreeUpToDate();
    }
}
Also used : com.mucommander.commons.file(com.mucommander.commons.file) ZipEntry(com.mucommander.commons.file.archive.zip.provider.ZipEntry)

Example 3 with ZipEntry

use of com.mucommander.commons.file.archive.zip.provider.ZipEntry in project mucommander by mucommander.

the class ZipArchiveFile method addEntry.

// ////////////////////////////////////////
// AbstractRWArchiveFile implementation //
// ////////////////////////////////////////
@Override
public synchronized OutputStream addEntry(final ArchiveEntry entry) throws IOException, UnsupportedFileOperationException {
    checkZipFile();
    final ZipEntry zipEntry = createZipEntry(entry);
    if (zipEntry.isDirectory()) {
        // Add the new directory entry to the zip file (physically)
        zipFile.addEntry(zipEntry);
        // Set the ZipEntry object into the ArchiveEntry
        entry.setEntryObject(zipEntry);
        // Declare the zip file and entries tree up-to-date and add the new entry to the entries tree
        finishAddEntry(entry);
        return null;
    } else {
        // Set the ZipEntry object into the ArchiveEntry
        entry.setEntryObject(zipEntry);
        return new FilteredOutputStream(zipFile.addEntry(zipEntry)) {

            @Override
            public void close() throws IOException {
                super.close();
                // Declare the zip file and entries tree up-to-date and add the new entry to the entries tree
                finishAddEntry(entry);
            }
        };
    }
}
Also used : FilteredOutputStream(com.mucommander.commons.io.FilteredOutputStream) ZipEntry(com.mucommander.commons.file.archive.zip.provider.ZipEntry)

Example 4 with ZipEntry

use of com.mucommander.commons.file.archive.zip.provider.ZipEntry in project mucommander by mucommander.

the class ZipArchiver method createEntry.

// ///////////////////////////
// Archiver implementation //
// ///////////////////////////
@Override
public OutputStream createEntry(String entryPath, FileAttributes attributes) throws IOException {
    // Start by closing current entry
    if (!firstEntry)
        zos.closeEntry();
    boolean isDirectory = attributes.isDirectory();
    // Create the entry and use the provided file's date
    ZipEntry entry = new ZipEntry(normalizePath(entryPath, isDirectory));
    // Use provided file's size and date
    long size = attributes.getSize();
    if (// Do not set size if file is directory or file size is unknown!
    !isDirectory && size >= 0)
        entry.setSize(size);
    entry.setTime(attributes.getDate());
    entry.setUnixMode(SimpleFilePermissions.padPermissions(attributes.getPermissions(), isDirectory ? FilePermissions.DEFAULT_DIRECTORY_PERMISSIONS : FilePermissions.DEFAULT_FILE_PERMISSIONS).getIntValue());
    // Add the entry
    zos.putNextEntry(entry);
    if (firstEntry)
        firstEntry = false;
    // Return the OutputStream that allows to write to the entry, only if it isn't a directory
    return isDirectory ? null : zos;
}
Also used : ZipEntry(com.mucommander.commons.file.archive.zip.provider.ZipEntry)

Example 5 with ZipEntry

use of com.mucommander.commons.file.archive.zip.provider.ZipEntry in project mucommander by mucommander.

the class ZipArchiveFile method getEntryInputStream.

@Override
public synchronized InputStream getEntryInputStream(ArchiveEntry entry, ArchiveEntryIterator entryIterator) throws IOException, UnsupportedFileOperationException {
    // If the underlying AbstractFile has random read access, use our own ZipFile implementation to read the entry
    if (file.isFileOperationSupported(FileOperation.RANDOM_READ_FILE)) {
        checkZipFile();
        ZipEntry zipEntry = (com.mucommander.commons.file.archive.zip.provider.ZipEntry) entry.getEntryObject();
        if (// Should not normally happen
        zipEntry == null)
            throw new IOException();
        return zipFile.getInputStream(zipEntry);
    } else // If the underlying AbstractFile doesn't have random read access, use java.util.InputStream to
    // read the entry. This is much slower than the former method as the file cannot be seeked and needs
    // to be traversed to locate the entry we're interested in.
    {
        // (unpack operation). In that case, we save the cost of looking for the entry in the archive.
        if (entryIterator != null && (entryIterator instanceof JavaUtilZipEntryIterator)) {
            ArchiveEntry currentEntry = ((JavaUtilZipEntryIterator) entryIterator).getCurrentEntry();
            if (currentEntry.getPath().equals(entry.getPath())) {
                // we don't want the ZipInputStream to be closed when the caller closes the entry's stream.
                return new FilterInputStream(((JavaUtilZipEntryIterator) entryIterator).getZipInputStream()) {

                    @Override
                    public void close() throws IOException {
                    // No-op
                    }
                };
            }
        // This is not the one, look for the entry from the beginning of the archive
        }
        // Iterate through the archive until we've found the entry
        java.util.zip.ZipInputStream zin = new java.util.zip.ZipInputStream(file.getInputStream());
        java.util.zip.ZipEntry zipEntry;
        String entryPath = entry.getPath();
        // Iterate until we find the entry we're looking for
        while ((zipEntry = zin.getNextEntry()) != null) if (// That's the one, return it
        zipEntry.getName().equals(entryPath))
            return zin;
        throw new IOException("Unknown Zip entry: " + entry.getName());
    }
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) ZipEntry(com.mucommander.commons.file.archive.zip.provider.ZipEntry) ArchiveEntry(com.mucommander.commons.file.archive.ArchiveEntry) ZipInputStream(java.util.zip.ZipInputStream) com.mucommander.commons.file(com.mucommander.commons.file)

Aggregations

ZipEntry (com.mucommander.commons.file.archive.zip.provider.ZipEntry)7 com.mucommander.commons.file (com.mucommander.commons.file)4 ZipInputStream (java.util.zip.ZipInputStream)2 ArchiveEntry (com.mucommander.commons.file.archive.ArchiveEntry)1 ArchiveEntryIterator (com.mucommander.commons.file.archive.ArchiveEntryIterator)1 FilteredOutputStream (com.mucommander.commons.io.FilteredOutputStream)1