【问题标题】:Unpack all jar and war files with in a zip file将所有 jar 和 war 文件解压缩到一个 zip 文件中
【发布时间】:2018-09-01 17:57:42
【问题描述】:

我有一个 zip 文件,其中包含,

  1. 战争文件
  2. jar 文件
  3. jar 文件中可能包含 jar 文件。

我需要解压缩一个包含上述文件类型的 zip 文件。同时,它也应该解压所有的jar或war文件。

有没有办法使用 shell 脚本或 java 来做到这一点?否则任何单个命令都可以执行所有这些操作。

【问题讨论】:

标签: java shell jar zip unzip


【解决方案1】:

试试下面的方法:

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
    }
  }
}

【讨论】:

  • 谢谢,我们如何使用 java 临时文件呢?
  • 文件 temp = File.createTempFile("temp-file-name", ".tmp"); //将创建临时文件和 temp.delete();将删除它。循环时(如果 current.endsWith(".tmp")){temp.delete()} 将起作用
猜你喜欢
  • 1970-01-01
  • 2021-04-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-10-07
  • 2015-09-29
  • 1970-01-01
  • 2021-05-16
相关资源
最近更新 更多