Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VFS-676 : change dependency from java.util.zip to Commons Compress #91

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.vfs2.provider.cczip;

import java.io.InputStream;
import java.util.HashSet;

import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.vfs2.FileName;
import org.apache.commons.vfs2.FileSystemException;
import org.apache.commons.vfs2.FileType;
import org.apache.commons.vfs2.provider.AbstractFileName;
import org.apache.commons.vfs2.provider.AbstractFileObject;

/**
* A file in a ZIP file system.
*
* @since 2.7.0
*/
public class CommonsCompressZipFileObject extends AbstractFileObject<CommonsCompressZipFileSystem> {

/**
* The ZipEntry.
*/
protected ZipArchiveEntry entry;
private final HashSet<String> children = new HashSet<>();
private FileType type;

protected CommonsCompressZipFileObject(final AbstractFileName name, final ZipArchiveEntry entry,
final CommonsCompressZipFileSystem fs,
final boolean zipExists) throws FileSystemException {
super(name, fs);
setZipEntry(entry);
if (!zipExists) {
type = FileType.IMAGINARY;
}
}

/**
* Sets the details for this file object.
*
* @param entry ZIP information related to this file.
*/
protected void setZipEntry(final ZipArchiveEntry entry) {
if (this.entry != null) {
return;
}

if (entry == null || entry.isDirectory()) {
type = FileType.FOLDER;
} else {
type = FileType.FILE;
}

this.entry = entry;
}

/**
* Attaches a child.
* <p>
* TODO: Shouldn't this method have package-only visibility? Cannot change this without breaking binary
* compatibility.
* </p>
*
* @param childName The name of the child.
*/
public void attachChild(final FileName childName) {
children.add(childName.getBaseName());
}

/**
* Determines if this file can be written to.
*
* @return {@code true} if this file is writable, {@code false} if not.
* @throws FileSystemException if an error occurs.
*/
@Override
public boolean isWriteable() throws FileSystemException {
return false;
}

/**
* Returns the file's type.
*/
@Override
protected FileType doGetType() {
return type;
}

/**
* Lists the children of the file.
*/
@Override
protected String[] doListChildren() {
try {
if (!getType().hasChildren()) {
return null;
}
} catch (final FileSystemException e) {
// should not happen as the type has already been cached.
throw new RuntimeException(e);
}

return children.toArray(new String[children.size()]);
}

/**
* Returns the size of the file content (in bytes). Is only called if {@link #doGetType} returns
* {@link FileType#FILE}.
*/
@Override
protected long doGetContentSize() {
return entry.getSize();
}

/**
* Returns the last modified time of this file.
*/
@Override
protected long doGetLastModifiedTime() throws Exception {
return entry.getTime();
}

/**
* Creates an input stream to read the file content from. Is only called if {@link #doGetType} returns
* {@link FileType#FILE}. The input stream returned by this method is guaranteed to be closed before this method is
* called again.
*/
@Override
protected InputStream doGetInputStream(final int bufferSize) throws Exception {
// VFS-210: zip allows to gather an input stream even from a directory and will
// return -1 on the first read. getType should not be expensive and keeps the tests
// running
if (!getType().hasContent()) {
throw new FileSystemException("vfs.provider/read-not-file.error", getName());
}

return getAbstractFileSystem().getZipFile().getInputStream(entry);
}

@Override
protected void doAttach() throws Exception {
getAbstractFileSystem().getZipFile();
}

@Override
protected void doDetach() throws Exception {
final CommonsCompressZipFileSystem afs = getAbstractFileSystem();
if (!afs.isOpen()) {
afs.close();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.vfs2.provider.cczip;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;

import org.apache.commons.vfs2.Capability;
import org.apache.commons.vfs2.FileName;
import org.apache.commons.vfs2.FileObject;
import org.apache.commons.vfs2.FileSystem;
import org.apache.commons.vfs2.FileSystemConfigBuilder;
import org.apache.commons.vfs2.FileSystemException;
import org.apache.commons.vfs2.FileSystemOptions;
import org.apache.commons.vfs2.FileType;
import org.apache.commons.vfs2.provider.AbstractFileName;
import org.apache.commons.vfs2.provider.AbstractLayeredFileProvider;
import org.apache.commons.vfs2.provider.LayeredFileName;

/**
* A file system provider for ZIP files. Provides read-only file systems.
*
* @since 2.7.0
*/
public class CommonsCompressZipFileProvider extends AbstractLayeredFileProvider {

/**
* The list of capabilities this provider supports
*/
protected static final Collection<Capability> capabilities = Collections.unmodifiableCollection(Arrays.asList(Capability.GET_LAST_MODIFIED, Capability.GET_TYPE, Capability.LIST_CHILDREN, Capability.READ_CONTENT,
Capability.URI, Capability.COMPRESS, Capability.VIRTUAL));

public CommonsCompressZipFileProvider() {
super();
}

/**
* Creates a layered file system. This method is called if the file system is
* not cached.
*
* @param scheme The URI scheme.
* @param file The file to create the file system on top of.
* @return The file system.
*/
@Override
protected FileSystem doCreateFileSystem(final String scheme, final FileObject file,
final FileSystemOptions fileSystemOptions) throws FileSystemException {
final AbstractFileName rootName = new LayeredFileName(scheme, file.getName(), FileName.ROOT_PATH,
FileType.FOLDER);
return new CommonsCompressZipFileSystem(rootName, file, fileSystemOptions);
}

@Override
public Collection<Capability> getCapabilities() {
return capabilities;
}

/**
* Return config builder.
*
* @return A config builder for ZipFileProvider.
* @see org.apache.commons.vfs2.provider.AbstractFileProvider#getConfigBuilder()
*/
@Override
public FileSystemConfigBuilder getConfigBuilder() {
return CommonsCompressZipFileSystemConfigBuilder.getInstance();
}
}
Loading