Java - 解压缩以不同方式压缩的文件
创建这个应用程序已经变得很痛苦!我想使用 Java 解压缩由许多不同应用程序创建的 .zip 文件: 使用我的 7-zip 效果非常好,使用某人的 winrar 来压缩文件完全搞乱了它们! 这是我的代码:
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);
}
}
示例: 我用这种方法解压了fie,它完全错过了里面的一个文件夹和某些文件......
Creating this application has become a pain in the ass! Using Java I want to unzip .zip files created by many different applications:
Using my 7-zip this works perfectly fine, Using somebodys winrar to compress the files completely messes them up!
Here's my code:
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);
}
}
Example:
I unzipped the fie using this method, it completely missed out a folder and certain files inside...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试不同的解压缩(解压缩)实现。 TrueZIP 是众所周知的。
Try a different unzip (decompression) implementation. TrueZIP is well known.