【发布时间】:2018-09-01 17:57:42
【问题描述】:
我有一个 zip 文件,其中包含,
- 战争文件
- jar 文件
- jar 文件中可能包含 jar 文件。
我需要解压缩一个包含上述文件类型的 zip 文件。同时,它也应该解压所有的jar或war文件。
有没有办法使用 shell 脚本或 java 来做到这一点?否则任何单个命令都可以执行所有这些操作。
【问题讨论】:
我有一个 zip 文件,其中包含,
我需要解压缩一个包含上述文件类型的 zip 文件。同时,它也应该解压所有的jar或war文件。
有没有办法使用 shell 脚本或 java 来做到这一点?否则任何单个命令都可以执行所有这些操作。
【问题讨论】:
试试下面的方法:
public void unzipFile(String zipFile) throws ZipException, IOException
{
int BUFFER = 2048;
ZipFile zip = new ZipFile(new File(zipFile));
String pathToMainFile = zipFile.substring(0, zipFile.length() - 4);
new File(pathToMainFile).mkdir();
Enumeration zipEntries = zip.entries();
while (zipEntries.hasMoreElements())
{
ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();
String current = entry.getName();
File dest = new File(pathToMainFile, current);
File outerParentSt = dest.getParentFile();
outerParentSt.mkdirs();
if (!entry.isDirectory())
{
BufferedInputStream is = new BufferedInputStream(zip.getInputStream(entry));
int currentByte;
byte data[] = new byte[BUFFER];
// write the file
FileOutputStream fos = new FileOutputStream(dest);
BufferedOutputStream destbuff = new BufferedOutputStream(fos,
BUFFER);
// r w to EOF
while ((currentByte = is.read(data, 0, BUFFER)) != -1) {
destbuff.write(data, 0, currentByte);
}
dest.flush();
dest.close();
is.close();
fos.close;
}
if (current.endsWith(".zip")) //or jar or war
{
unzipFile(dest.getAbsolutePath()); //recursively
}
}
}
【讨论】: