// 循环遍历目录结构中的文件并添加至tar的输出流
 public static void addFiles(TarOutputStream tout, String folderPath) {
   File srcPath = new File(folderPath);
   int length = srcPath.listFiles().length;
   byte[] buf = new byte[1024]; // 设定读入缓冲区尺寸
   File[] files = srcPath.listFiles();
   try {
     for (int i = 0; i < length; i++) {
       if (files[i].isFile()) {
         System.out.println("file:" + files[i].getName());
         String filename = srcPath.getPath() + File.separator + files[i].getName();
         // 打开需压缩文件作为文件输入流
         FileInputStream fin = new FileInputStream(filename); // filename是文件全路径
         TarEntry tarEn = new TarEntry(files[i]); // 此处必须使用new
         // TarEntry(File
         // file);
         // tarEn.setName(files[i].getName());
         // //此处需重置名称,默认是带全路径的,否则打包后会带全路径
         tout.putNextEntry(tarEn);
         int num;
         while ((num = fin.read(buf)) != -1) {
           tout.write(buf, 0, num);
         }
         tout.closeEntry();
         fin.close();
       } else {
         System.out.println(files[i].getPath());
         addFiles(tout, files[i].getPath());
       }
     }
   } catch (FileNotFoundException e) {
     System.out.println(e);
   } catch (IOException e) {
     System.out.println(e);
   }
 }
  // 不能对每层都包含文件和目录的多层次目录结构打包
  public static void compressedFiles_Gzip(String folderPath, String targzipFilePath) {
    File srcPath = new File(folderPath);
    int length = srcPath.listFiles().length;
    byte[] buf = new byte[1024]; // 设定读入缓冲区尺寸
    File[] files = srcPath.listFiles();
    try {
      File targetFile = new File(targzipFilePath);
      File parent = targetFile.getParentFile();
      if (!parent.exists()) {
        parent.mkdirs();
      }
      // 建立压缩文件输出流
      FileOutputStream fout = new FileOutputStream(targetFile);
      // 建立tar压缩输出流
      TarOutputStream tout = new TarOutputStream(fout);
      for (int i = 0; i < length; i++) {
        String filename = srcPath.getPath() + File.separator + files[i].getName();
        // 打开需压缩文件作为文件输入流
        FileInputStream fin = new FileInputStream(filename); // filename是文件全路径
        TarEntry tarEn = new TarEntry(files[i]); // 此处必须使用new
        // TarEntry(File
        // file);
        // tarEn.setName(files[i].getName());
        // //此处需重置名称,默认是带全路径的,否则打包后会带全路径
        tout.putNextEntry(tarEn);
        int num;
        while ((num = fin.read(buf)) != -1) {
          tout.write(buf, 0, num);
        }
        tout.closeEntry();
        fin.close();
      }

      tout.close();
      fout.close();

      // 建立压缩文件输出流
      FileOutputStream gzFile = new FileOutputStream(targzipFilePath + ".gz");
      // 建立gzip压缩输出流
      GZIPOutputStream gzout = new GZIPOutputStream(gzFile);
      // 打开需压缩文件作为文件输入流
      FileInputStream tarin = new FileInputStream(targzipFilePath); // targzipFilePath是文件全路径
      int len;
      while ((len = tarin.read(buf)) != -1) {
        gzout.write(buf, 0, len);
      }
      gzout.close();
      gzFile.close();
      tarin.close();
      // 因为只要tar.gz文件,所以删除.tar文件
      del(targzipFilePath);
    } catch (FileNotFoundException e) {
      System.out.println(e);
    } catch (IOException e) {
      System.out.println(e);
    }
  }
Пример #3
0
  private static void addEntry(
      final String pName, final String pContent, final TarOutputStream pOutput) throws IOException {
    final byte[] data = pContent.getBytes("UTF-8");

    final TarEntry entry = new TarEntry("./" + pName);
    entry.setSize(data.length);
    entry.setNames("root", "root");

    pOutput.putNextEntry(entry);
    pOutput.write(data);
    pOutput.closeEntry();
  }
Пример #4
0
  @Test(timeout = 30000)
  public void testUnTar() throws IOException {
    setupDirs();

    // make a simple tar:
    final File simpleTar = new File(del, FILE);
    OutputStream os = new FileOutputStream(simpleTar);
    TarOutputStream tos = new TarOutputStream(os);
    try {
      TarEntry te = new TarEntry("/bar/foo");
      byte[] data = "some-content".getBytes("UTF-8");
      te.setSize(data.length);
      tos.putNextEntry(te);
      tos.write(data);
      tos.closeEntry();
      tos.flush();
      tos.finish();
    } finally {
      tos.close();
    }

    // successfully untar it into an existing dir:
    FileUtil.unTar(simpleTar, tmp);
    // check result:
    assertTrue(new File(tmp, "/bar/foo").exists());
    assertEquals(12, new File(tmp, "/bar/foo").length());

    final File regularFile = new File(tmp, "QuickBrownFoxJumpsOverTheLazyDog");
    regularFile.createNewFile();
    assertTrue(regularFile.exists());
    try {
      FileUtil.unTar(simpleTar, regularFile);
      assertTrue("An IOException expected.", false);
    } catch (IOException ioe) {
      // okay
    }
  }