0
創建此應用程序已成爲一個痛苦的屁股!使用Java我想解壓縮由許多不同的應用程序創建的.zip文件: 使用我的7-zip這工作得很好,使用somebodys winrar壓縮文件完全混淆了它們! 這裏是我的代碼:Java - 解壓縮文件的壓縮方式不同
public static void ExtractModZip(File Zip, File Dest) {
try {
if (Zip.getName().toLowerCase().endsWith(".zip")) {
}
ZipFile zip = new ZipFile(Zip);
System.out.println(zip.getName() + " opened.");
Enumeration entries = zip.entries();
String ModName = Zip.getName().substring(0, Zip.getName().length() - 4);
File base = new File(Dest + File.separator + ModName);
base.mkdirs();
InputStream entryStream = null;
FileOutputStream fos = null;
while (entries.hasMoreElements()) {
ZipEntry entry = (ZipEntry) entries.nextElement();
entryStream = zip.getInputStream(entry);
String entryName = entry.getName().replace('/', File.separatorChar);
entryName = entryName.replace('\\', File.separatorChar);
if (!entry.isDirectory()) {
File file = new File(base + File.separator + entryName);
File Base = new File(base + File.separator);
if (!Base.exists()) {
Base.mkdirs();
}
fos = new FileOutputStream(file);
try {
// Allocate a buffer for reading the entry data.
byte[] buffer = new byte[1024];
int bytesRead;
// Read the entry data and write it to the output file.
while ((bytesRead = entryStream.read(buffer)) != -1) {
fos.write(buffer, 0, bytesRead);
}
System.out.println(entry.getName() + " extracted.");
} catch (Exception e) {
e.printStackTrace();
}
} else {
File file = new File(base + File.separator + entryName);
file.mkdir();
}
}
fos.close();
entryStream.close();
} catch (ZipException ex) {
Logger.getLogger(fileUtils.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(fileUtils.class.getName()).log(Level.SEVERE, null, ex);
}
}
例子: 我用這個方法解壓縮後的外商投資企業,它完全錯過了一個文件夾,某些文件裏面...