У меня возникла проблема с использованием библиотеки сжатия commons для создания каталога tar.gz. У меня есть структура каталогов, которая выглядит следующим образом.
parent/
child/
file1.raw
fileN.raw
Я использую следующий код для сжатия. Он работает нормально без исключений. Однако, когда я пытаюсь распаковать этот tar.gz, я получаю один файл с именем «childDirToCompress». Это правильный размер, поэтому файлы были явно добавлены друг к другу в процессе тарирования. Желаемым результатом будет каталог. Я не могу понять, что я делаю неправильно. Может ли какой-нибудь мудрый компрессор общих ресурсов направить меня на правильный путь?
CreateTarGZ() throws CompressorException, FileNotFoundException, ArchiveException, IOException {
File f = new File("parent");
File f2 = new File("parent/childDirToCompress");
File outFile = new File(f2.getAbsolutePath() + ".tar.gz");
if(!outFile.exists()){
outFile.createNewFile();
}
FileOutputStream fos = new FileOutputStream(outFile);
TarArchiveOutputStream taos = new TarArchiveOutputStream(new GZIPOutputStream(new BufferedOutputStream(fos)));
taos.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_STAR);
taos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
addFilesToCompression(taos, f2, ".");
taos.close();
}
private static void addFilesToCompression(TarArchiveOutputStream taos, File file, String dir) throws IOException{
taos.putArchiveEntry(new TarArchiveEntry(file, dir));
if (file.isFile()) {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
IOUtils.copy(bis, taos);
taos.closeArchiveEntry();
bis.close();
}
else if(file.isDirectory()) {
taos.closeArchiveEntry();
for (File childFile : file.listFiles()) {
addFilesToCompression(taos, childFile, file.getName());
}
}
}
TarGzFile
, для рекурсивного сжатия одного или нескольких файлов и каталогов в файл *.tar.gz. - person spongebob   schedule 10.06.2018