java普通工程打war包_用java打tar包

写了一个打tar包的工具类,可以递归打整个目录。没有中文文件名的问题。

引入依赖:

  org.apache.commons  commons-compress  1.20

代码:

public class TarUtil {  private static List files = new ArrayList();  public static final String[] paths = new String[]{          "D:图库",          "D:y.jpg",          "D:一个合同.pdf"  };  public static final File target = new File("D:打包测试.tar");  /**    * @return File 返回打包后的文件    * @throws    * @Title: pack    * @Description: 将一组文件打成tar包    */  public static File pack() {      long startTime = System.currentTimeMillis();      FileOutputStream out = null;      try {          out = new FileOutputStream(target);      } catch (FileNotFoundException e1) {          e1.printStackTrace();      }      TarArchiveOutputStream os = new TarArchiveOutputStream(out);      os.setLongFileMode(2);      System.out.println("***************开始打" + target + "包****************");      int fileCount = 0;      for (File file : getSourcesFile()) {          try {              os.putArchiveEntry(new TarArchiveEntry(file));              IOUtils.copy(new FileInputStream(file), os);              os.closeArchiveEntry();              fileCount++;          } catch (FileNotFoundException e) {              System.out.print("****创建" + file.getName() + "******异常");              e.printStackTrace();          } catch (IOException e) {              e.printStackTrace();          }      }      if (os != null) {          try {              os.flush();              os.close();          } catch (IOException e) {              e.printStackTrace();          }      }      long endTime = System.currentTimeMillis();      System.out.println("***************打" + target + "包结束:用时 "              + (endTime - startTime) + "毫秒,共" + fileCount + "个文件。");      return target;  }  public static List getSourcesFile() {      for (int i = 0; i < paths.length; i++) {          try {              File file = new File(paths[i]);              getFilesByDirectory(file);          } catch (Exception e) {              System.out.println("-----创建文件" + paths[i] + "异常------");              e.printStackTrace();          }      }      return files;  }  public static void getFilesByDirectory(File file) {      if (file.isDirectory()) {          File[] fileArrays = file.listFiles();          for (File f : fileArrays) {              getFilesByDirectory(f);          }      } else {          files.add(file);      }  }  public static void main(String[] args) {      pack();  }}

效果:

5d9792214f4347edb745027184ba4ca7.png

源码路径:

需要代码地址的,请关注公-众-号 查看: 技术笔记与开源分享

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值