首页

关于enough-webprocessor源码包JarUtil工具类对jar打包解压缩进行操作

标签:JarUtil,jar工具类,enough,webprocessor     发布时间:2018-03-16   

一、前言

关于enough-webprocessor.jar源码包中的de.enough.webprocessor.util.JarUtil包工具类,对文件打包Jar文件、对jar文件进行解压缩等操作。

二、源码说明

package de.enough.webprocessor.util;@b@@b@import java.io.File;@b@import java.io.FileInputStream;@b@import java.io.FileNotFoundException;@b@import java.io.FileOutputStream;@b@import java.io.IOException;@b@import java.io.InputStream;@b@import java.util.Enumeration;@b@import java.util.jar.JarEntry;@b@import java.util.jar.JarFile;@b@import java.util.jar.JarOutputStream;@b@import java.util.zip.CRC32;@b@@b@public final class JarUtil@b@{@b@  public static void jar(File sourceDir, File target, boolean compress)@b@    throws FileNotFoundException, IOException@b@  {@b@    if (!(target.getParentFile().exists())) {@b@      target.getParentFile().mkdirs();@b@    }@b@@b@    JarOutputStream out = new JarOutputStream(@b@      new FileOutputStream(target));@b@    if (compress)@b@      out.setLevel(8);@b@    else {@b@      out.setLevel(0);@b@    }@b@@b@    CRC32 crc = new CRC32();@b@    byte[] buffer = new byte[1048576];@b@@b@    File[] fileNames = sourceDir.listFiles();@b@    int sourceDirLength = sourceDir.getAbsolutePath().length() + 1;@b@    for (int i = 0; i < fileNames.length; ++i) {@b@      File file = fileNames[i];@b@      addFile(file, out, crc, sourceDirLength, buffer);@b@    }@b@    out.close();@b@  }@b@@b@  private static void addFile(File file, JarOutputStream out, CRC32 crc, int sourceDirLength, byte[] buffer)@b@    throws FileNotFoundException, IOException@b@  {@b@    if (file.isDirectory()) {@b@      File[] fileNames = file.listFiles();@b@      for (int i = 0; i < fileNames.length; ++i)@b@        addFile(fileNames[i], out, crc, sourceDirLength, buffer);@b@    }@b@    else {@b@      int read;@b@      String entryName = file.getAbsolutePath().substring(sourceDirLength);@b@      JarEntry entry = new JarEntry(entryName);@b@      out.putNextEntry(entry);@b@@b@      FileInputStream in = new FileInputStream(file);@b@@b@      long size = 0L;@b@      while ((read = in.read(buffer)) != -1) {@b@        crc.update(buffer, 0, read);@b@        out.write(buffer, 0, read);@b@        size += read;@b@      }@b@      entry.setCrc(crc.getValue());@b@      entry.setSize(size);@b@      in.close();@b@      out.closeEntry();@b@      crc.reset();@b@    }@b@  }@b@@b@  public static void unjar(File jarFile, File targetDir)@b@    throws FileNotFoundException, IOException@b@  {@b@    if (targetDir.exists()) {@b@      targetDir.delete();@b@    }@b@@b@    targetDir.mkdirs();@b@@b@    String targetPath = targetDir.getAbsolutePath() + File.separatorChar;@b@    byte[] buffer = new byte[1048576];@b@    JarFile input = new JarFile(jarFile, false, 1);@b@    Enumeration enum = input.entries();@b@    while (enum.hasMoreElements()) {@b@      int read;@b@      JarEntry entry = (JarEntry)enum.nextElement();@b@      String path = targetPath + entry.getName();@b@      File file = new File(path);@b@      if (!(file.getParentFile().exists()))@b@        file.getParentFile().mkdirs();@b@@b@      FileOutputStream out = new FileOutputStream(file);@b@      InputStream in = input.getInputStream(entry);@b@@b@      while ((read = in.read(buffer)) != -1)@b@        out.write(buffer, 0, read);@b@@b@      in.close();@b@      out.close();@b@    }@b@  }@b@}