支持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();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值