Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FORGE-2308: Introduces ZipFileResource for Zip file operations
- Loading branch information
Showing
8 changed files
with
541 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
resources/api/src/main/java/org/jboss/forge/addon/resource/zip/ZipFileResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
resources/impl/src/main/java/org/jboss/forge/addon/resource/zip/ZipFileResourceEntry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
|
||
} |
47 changes: 47 additions & 0 deletions
47
...urces/impl/src/main/java/org/jboss/forge/addon/resource/zip/ZipFileResourceGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
Oops, something went wrong.