Skip to content

Commit

Permalink
FORGE-2308: Introduces ZipFileResource for Zip file operations
Browse files Browse the repository at this point in the history
  • Loading branch information
gastaldi committed Jul 13, 2015
1 parent 384b532 commit d445900
Show file tree
Hide file tree
Showing 8 changed files with 541 additions and 1 deletion.
14 changes: 14 additions & 0 deletions resources/README.asciidoc
Expand Up @@ -249,3 +249,17 @@ Collection [
ResourceCreated: <FileResource<?>>
]
----

Built-in support for zip files::
The resources API provides a built-in support for zip files through the `ZipFileResource` object:
+
[source:java]
----
...
ZipFileResource resource = resourceFactory.create(ZipFileResource.class, new File("file.zip"));
DirectoryResource outputDir = ...;
// Extract the zip contents to the specified directory
resource.extractTo(outputDir);
----
+

@@ -0,0 +1,57 @@
/**
* Copyright 2015 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Eclipse Public License version 1.0, available at
* http://www.eclipse.org/legal/epl-v10.html
*/

package org.jboss.forge.addon.resource.zip;

import org.jboss.forge.addon.resource.DirectoryResource;
import org.jboss.forge.addon.resource.FileResource;
import org.jboss.forge.addon.resource.Resource;

/**
* Handles a Zip file and provide operations related to it
*
* @author <a href="mailto:ggastald@redhat.com">George Gastaldi</a>
*/
public interface ZipFileResource extends FileResource<ZipFileResource>
{
/**
* Unzip all files to the specified {@link DirectoryResource}.
*
* @param directoryResource the target directory
*/
void extractTo(DirectoryResource directoryResource);

/**
* Add the specified resources to this Zip file
*
* @param resources the {@link FileResource} instances to be added
* @return this {@link ZipFileResource} instance, for method chaining
*/
ZipFileResource add(FileResource<?>... resources);

/**
* Add the specified {@link Resource} to this Zip file with the given name
*
* @param name the file name inside the Zip
* @param resource the {@link Resource} instance to be added
* @return this {@link ZipFileResource} instance, for method chaining
*/
ZipFileResource add(String name, Resource<?> resource);

/**
* Sets the password for this {@link ZipFileResource}
*
* @param password the password to be used when reading this file
* @return this {@link ZipFileResource} instance, for method chaining
*/
ZipFileResource setPassword(char[] password);

/**
* @return true if this Zip file is encrypted
*/
boolean isEncrypted();
}
9 changes: 8 additions & 1 deletion resources/impl/pom.xml
@@ -1,4 +1,5 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.jboss.forge.addon</groupId>
Expand Down Expand Up @@ -42,5 +43,11 @@
<artifactId>connector-api</artifactId>
<version>1.5</version>
</dependency>
<!-- Needed for ZipFileResource -->
<dependency>
<groupId>net.lingala.zip4j</groupId>
<artifactId>zip4j</artifactId>
<version>1.3.2</version>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,97 @@
/**
* Copyright 2015 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Eclipse Public License version 1.0, available at
* http://www.eclipse.org/legal/epl-v10.html
*/

package org.jboss.forge.addon.resource.zip;

import java.io.InputStream;
import java.util.Collections;
import java.util.List;

import org.jboss.forge.addon.resource.Resource;
import org.jboss.forge.addon.resource.ResourceException;
import org.jboss.forge.addon.resource.ResourceFactory;
import org.jboss.forge.addon.resource.VirtualResource;
import org.jboss.forge.furnace.util.Assert;

import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.model.FileHeader;

/**
* Entries for a {@link ZipFileResource}
*
* @author <a href="mailto:ggastald@redhat.com">George Gastaldi</a>
*/
public class ZipFileResourceEntry extends VirtualResource<ZipFileResource>
{
private final FileHeader fileHeader;

public ZipFileResourceEntry(ResourceFactory factory, ZipFileResource parent, FileHeader fileHeader)
{
super(factory, parent);
Assert.notNull(fileHeader, "File header should not be null");
this.fileHeader = fileHeader;
}

@Override
public boolean delete()
{
try
{
getZipFile().removeFile(fileHeader);
}
catch (ZipException e)
{
throw new ResourceException("Error while deleting zip entry", e);
}
return true;
}

@Override
public InputStream getResourceInputStream()
{
try
{
return getZipFile().getInputStream(fileHeader);
}
catch (ZipException e)
{
throw new ResourceException("Error while fetching zip contents", e);
}
}

@Override
public boolean delete(boolean recursive) throws UnsupportedOperationException
{
return delete();
}

@Override
public String getName()
{
return fileHeader.getFileName();
}

@Override
public ZipFileResource getUnderlyingResourceObject()
{
return (ZipFileResource) getParent();
}

@Override
protected List<Resource<?>> doListResources()
{
return Collections.emptyList();
}

private ZipFile getZipFile()
{
ZipFileResourceImpl impl = (ZipFileResourceImpl) getParent();
return impl.getZipFile();
}

}
@@ -0,0 +1,47 @@
/**
* Copyright 2015 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Eclipse Public License version 1.0, available at
* http://www.eclipse.org/legal/epl-v10.html
*/

package org.jboss.forge.addon.resource.zip;

import java.io.File;
import java.io.File;

import org.jboss.forge.addon.resource.Resource;
import org.jboss.forge.addon.resource.ResourceFactory;
import org.jboss.forge.addon.resource.ResourceGenerator;

/**
* Implementation of {@link ResourceGenerator} for {@link ZipFileResource}
*
* @author <a href="mailto:ggastald@redhat.com">George Gastaldi</a>
*/
public class ZipFileResourceGenerator implements ResourceGenerator<ZipFileResource, File>
{
@Override
public boolean handles(Class<?> type, Object resource)
{
if (resource instanceof File)
{
return (type == ZipFileResource.class || ((File) resource).getName().toLowerCase().endsWith(".zip"));
}
return false;
}

@SuppressWarnings("unchecked")
@Override
public <T extends Resource<File>> T getResource(ResourceFactory factory, Class<ZipFileResource> type, File resource)
{
return (T) new ZipFileResourceImpl(factory, resource);
}

@Override
public <T extends Resource<File>> Class<?> getResourceType(ResourceFactory factory, Class<ZipFileResource> type,
File resource)
{
return ZipFileResource.class;
}
}

0 comments on commit d445900

Please sign in to comment.