支持Window和Linux下tar.gz文件压缩

最近做文件压缩,最后在Window下压缩解压后都可以,可是在Windows下压缩后到Linux下用命令解压(tar zxvf tarname.tar.gz -C unFolderName)就出现问题了,文件名包括在单独的文件中(05\foldername\file.txt)。

后来发现是压缩单独的文件时出错了,
TarArchiveEntry tae = new TarArchiveEntry(file, finalFileName);
这样写就出现了以上错误,改成以下一个参数的就可以了
TarArchiveEntry tae = new TarArchiveEntry(finalFileName);


另外就是,压缩文件夹和压缩单独文件分开



private void compressPerDayFolder(String currentDay) throws IOException,
      CompressorException {
    // prepare current need to compress day source file folder
    StringBuffer sourceFileDirPath = new StringBuffer(compressMonthDirPath)
        .append("/").append(currentDay);
    String sourceFileDirPathStr = sourceFileDirPath.toString().replace("/",
        File.separator);
    File sourceFileDir = new File(sourceFileDirPathStr);

    // prepare current Day directory for following use
    compressDayDirPath = sourceFileDirPathStr;
    compressDayDirPathLengthAddOne = sourceFileDirPathStr.length() + 1;

    // prepare current day finally compressed file
    StringBuffer targetFileName = new StringBuffer("snapshot_archive_")
        .append(compressYear).append(compressMonth).append(currentDay)
        .append(".tar.gz");
    StringBuffer targetFilePath = new StringBuffer(compressMonthDirPath)
        .append(File.separator).append(targetFileName);
    File targetFile = new File(targetFilePath.toString());

    if (sourceFileDir.isDirectory()) {
      File[] sourceFileArr = sourceFileDir.listFiles();

      // compress all files in this day folder
      packAndCompressSnapshotXmlFiles(sourceFileArr, targetFile);
    } else {
      getLogger().error("This is not folder: " + sourceFileDirPathStr);
    }
  }

private void packAndCompressSnapshotXmlFiles(File[] sourceFileArr,
      File targetFile) throws IOException,
      CompressorException {
    FileOutputStream fileOutputStream = null;
    CompressorOutputStream gzippedOut = null;
    TarArchiveOutputStream taos = null;

    try {
      fileOutputStream = FileUtils.openOutputStream(targetFile);

      gzippedOut = new CompressorStreamFactory().createCompressorOutputStream(
          CompressorStreamFactory.GZIP, fileOutputStream);

      taos = new TarArchiveOutputStream(gzippedOut);
      // iteration all files in this day folder to compress
      for (File hourDirFile : sourceFileArr) {
        compressSnapshotFolders(hourDirFile, taos);
      }
    } catch (IOException ioe) {
      throw ioe;
    } catch (CompressorException ce) {
      throw ce;
    } finally {
      if (taos != null) {
        try {
          taos.close();
        } catch (IOException e) {
        }
      }

      if (gzippedOut != null) {
        try {
          gzippedOut.close();
        } catch (IOException e) {
        }
      }
    }

  }

  private void compressSnapshotFolders(File file,
      TarArchiveOutputStream taos) throws IOException {
    if (file.isDirectory()) {
      parkAndCompressPerDir(file, taos);
      
      File[] subFileArr = file.listFiles();
      for (File subFile : subFileArr) {
        compressSnapshotFolders(subFile, taos);
      }
    } else {
      parkAndCompressPerFile(file, taos);
    }
  }

  private void parkAndCompressPerFile(File file, TarArchiveOutputStream taos)
      throws IOException {
    // substring the file name path(forget all before the day folder, not
    // including the day and file separator)
    String fileAbstPath = file.getAbsolutePath();
    String finalFileName = fileAbstPath.substring(fileAbstPath
        .indexOf(compressDayDirPath) + compressDayDirPathLengthAddOne);

    // this file will in this folders if the name string including
    // current operation system file separator
    TarArchiveEntry tae = new TarArchiveEntry(finalFileName);
    tae.setSize(file.length());
    taos.putArchiveEntry(tae);
    taos.write(FileUtils.readFileToByteArray(file));
    taos.closeArchiveEntry();
  }
  
  private void parkAndCompressPerDir(File file, TarArchiveOutputStream taos)
      throws IOException {
    // substring the file name path(forget all before the day folder, not
    // including the day and file separator)
    String fileAbstPath = file.getAbsolutePath();
    String finalFileName = fileAbstPath.substring(fileAbstPath
        .indexOf(compressDayDirPath) + compressDayDirPathLengthAddOne);

    // this file will in this folders if the name string including
    // current operation system file separator
    TarArchiveEntry tae = new TarArchiveEntry(finalFileName + File.separator);
    taos.putArchiveEntry(tae);
    taos.closeArchiveEntry();
  }

你可能感兴趣的:(window)