Hi I am quite new to zip format, and I used the Java's util implementation to unzip a file, However it throws a ZipException everytime I try to open the file. I checked to see if the file was corrupted and it is not because I can open it using winRar.
So I moved on and tried appache.commons.vfs
package to do the same thing and that too resulted in a failure.
As a final try I tried the library 7-zip-jbinding and that is able to read the contents of the Zip archive but I am not able to extract it.
Here is my code for native java implementation:
BufferedOutputStream dest = null;
FileInputStream fis = new FileInputStream(archive);
ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
ZipEntry entry;
File destFile;
destinationDir.mkdir();
while ((entry = zis.getNextEntry()) != null) {
String currentfileName = entry.getName();
destFile = new File(destinationDir, currentfileName);
File destinationParent = destFile.getParentFile();
destinationParent.mkdirs();
if (!entry.isDirectory()) {
int currentByte;
byte data[] = new byte[BUFFER_SIZE];
FileOutputStream fos = new FileOutputStream(destFile);
dest = new BufferedOutputStream(fos, BUFFER_SIZE);
while ((currentByte = zis.read(data, 0, BUFFER_SIZE)) != -1) {
dest.write(data, 0, currentByte);
}
dest.flush();
dest.close();
}
}
fis.close();
zis.close();
My code for appache commons vfs
public void unpack(final File outputDir,URI packLocation) throws IOException
{
this.packLocation = packLocation;
this.fileSystemManager = VFS.getManager();
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();
}
}
Is there something very obvious that I am missing? And if anyone has used 7-zip-jbinding for extracting a zip file into file system please do let me know how it is done.
EDIT 1:
While executing a unzip in mac terminal of that file it showed me the following warning
warning : 84 extra bytes at beginning or within zipfile .. Can this somehow affect Zip input Stream ?
EDIT 2:
The extrabytes in the zip file seemed to screw up the zip input stream .. Using unzip on the terminal shows the number of extra bytes that are added to the zip file . I Used a RandomAccessFile to skip (n) bytes and the Zip Input Stream Started working fine .. Now all i need to do is find a way to remove the extra bytes in Zip File ..
The edited code is pasted below for anyone who would benifit from it in the future .
final int BUFFER_SIZE = 1024;
RandomAccessFile randomAccessFile = new RandomAccessFile(archive, "r");
randomAccessFile.skipBytes(84); // The extra bytes
FileChannel channel = randomAccessFile.getChannel();
BufferedOutputStream dest = null;
ZipInputStream zis = new ZipInputStream(new BufferedInputStream(Channels.newInputStream(channel)));
ZipEntry entry;
File destFile;
destinationDir.mkdir();
while ((entry = zis.getNextEntry()) != null) {
String currentfileName = entry.getName();
destFile = new File(destinationDir, currentfileName);
File destinationParent = destFile.getParentFile();
destinationParent.mkdirs();
if (!entry.isDirectory()) {
int currentByte;
byte data[] = new byte[BUFFER_SIZE];
FileOutputStream fos = new FileOutputStream(destFile);
dest = new BufferedOutputStream(fos, BUFFER_SIZE);
while ((currentByte = zis.read(data, 0, BUFFER_SIZE)) != -1) {
dest.write(data, 0, currentByte);
}
dest.flush();
dest.close();
}
}
zis.close();
If anyone knows a way to skip the extra bytes I am all ears :)