您好我對zip格式很陌生,我使用Java的util實現來解壓縮一個文件,但是每次嘗試打開文件時都會拋出一個ZipException異常。我查看了文件是否損壞,這不是因爲我可以用winRar打開它。在Java中解壓縮錯誤
所以我繼續前進,並嘗試appache.commons.vfs
包做同樣的事情,也導致失敗。
作爲最後的嘗試,我嘗試了庫7-zip-jbinding,它能夠讀取Zip存檔的內容,但我無法解壓縮它。
這裏是我的本地Java實現代碼:
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();
我的代碼appache公共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();
}
}
有什麼很明顯的,我很想念?如果有人使用7-zip-jbinding將文件解壓縮到文件系統中,請讓我知道它是如何完成的。
編輯1:
在執行該文件中的MAC終端解壓它給我下面的警告
警告:在開始或壓縮文件內的84個額外的字節。可這在某種程度上影響郵編輸入流?
編輯2:
在zip文件的extrabytes似乎把螺絲擰拉鍊輸入流..在終端上使用解壓示出被添加到zip文件額外的字節數。我用一個RandomAccessFile跳過(n)個字節和Zip輸入流開始工作正常..現在我需要做的是找到一種方法來刪除Zip文件中的額外字節..
編輯後的代碼粘貼在下面對於那些將來從中受益的人來說。
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();
如果有人知道的方式來跳過額外的字節,我所有的耳朵:)
檢查[this](http:// stackoverflow。com/questions/325202/java-util-zip-zipexception-error-in-opening-zip-file)post –
這就是我第一篇文章,我看着..它沒有幫助..謝謝你指向我,雖然。 – Chris