如何使用org.apache.commons.compress库来tar目录并保留目录结构?
通过我在下面的工作,我只是得到一个包装,一切都扁平化.
谢谢!
这是我一直在尝试的,它不起作用.
public static void createTar(final String tarName, final List pathEntries) throws IOException {
OutputStream tarOutput = new FileOutputStream(new File(tarName));
ArchiveOutputStream tarArchive = new TarArchiveOutputStream(tarOutput);
List files = new ArrayList();
for (File file : pathEntries) {
files.addAll(recurseDirectory(file));
}
for (File file : files) {
TarArchiveEntry tarArchiveEntry = new TarArchiveEntry(file, file.getName());
tarArchiveEntry.setSize(file.length());
tarArchive.putArchiveEntry(tarArchiveEntry);
FileInputStream fileInputStream = new FileInputStream(file);
IOUtils.copy(fileInputStream, tarArchive);
fileInputStream.close();
tarArchive.closeArchiveEntry();
}
tarArchive.finish();
tarOutput.close();
}
public static List recurseDirectory(final File directory) {
List files = new ArrayList();
if (directory != null && directory.isDirectory()) {
for (File file : directory.listFiles()) {
if (file.isDirectory()) {
files.addAll(recurseDirectory(file));
} else {
files.add(file);
}
}
}
return files;
}