Apache VFS 2.0 is strange but super powerful. Uncompress a file at a given URI to a given output location:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-vfs2</artifactId>
<version>2.0</version>
</dependency> |
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-vfs2</artifactId>
<version>2.0</version>
</dependency>
import java.io.File;
import java.io.IOException;
import java.net.URI;
import org.apache.commons.vfs2.AllFileSelector;
import org.apache.commons.vfs2.FileObject;
import org.apache.commons.vfs2.FileSystemException;
import org.apache.commons.vfs2.FileSystemManager;
import org.apache.commons.vfs2.VFS;
public class ZipfileUnpacker
{
private final FileSystemManager fileSystemManager;
private final URI packLocation;
public ZipfileUnpacker(final URI packLocation) throws FileSystemException
{
this.fileSystemManager = VFS.getManager();
this.packLocation = packLocation;
}
@Override
public void unpack(final File outputDir) throws IOException
{
outputDir.mkdirs();
final FileObject packFileObject = fileSystemManager.resolveFile(packLocation.toString());
try
{
final FileObject zipFileSystem = fileSystemManager.createFileSystem(packFileObject);
try
{
fileSystemManager.toFileObject(outputDir).copyFrom(zipFileSystem, new AllFileSelector());
}
finally
{
zipFileSystem.close();
}
}
finally
{
packFileObject.close();
}
}
} |
import java.io.File;
import java.io.IOException;
import java.net.URI;
import org.apache.commons.vfs2.AllFileSelector;
import org.apache.commons.vfs2.FileObject;
import org.apache.commons.vfs2.FileSystemException;
import org.apache.commons.vfs2.FileSystemManager;
import org.apache.commons.vfs2.VFS;
public class ZipfileUnpacker
{
private final FileSystemManager fileSystemManager;
private final URI packLocation;
public ZipfileUnpacker(final URI packLocation) throws FileSystemException
{
this.fileSystemManager = VFS.getManager();
this.packLocation = packLocation;
}
@Override
public void unpack(final File outputDir) throws IOException
{
outputDir.mkdirs();
final FileObject packFileObject = fileSystemManager.resolveFile(packLocation.toString());
try
{
final FileObject zipFileSystem = fileSystemManager.createFileSystem(packFileObject);
try
{
fileSystemManager.toFileObject(outputDir).copyFrom(zipFileSystem, new AllFileSelector());
}
finally
{
zipFileSystem.close();
}
}
finally
{
packFileObject.close();
}
}
}
Hi
Thanks for your code, its very useful. how to recursively unzip the zip files contained in a zip file using commons vfs? Please help
Regards
senthil
Just use this code again on the zip files that you got out of your zip file.
Hi,
Thanks for code, can you please tell to which class exactly your class ZipfileUnpacker extending as It is overriding unpack method. Currently I am not able to unzip file with above code.
Thanks