当我将文件tar压缩时,其抛出异常为“太长(> 100字节)TarArchiveOutputStream”。请指导我插入setLongFileMode(TarOutputStream.LONGFILE_GNU);在这个程序中。
private static void zipFilesRecursively(File baseDir, File source,TarArchiveOutputStream out) throws IOException {
if (source.isFile()) {
System.out.println("Adding File: "+ baseDir.toURI().relativize(source.toURI()).getPath());
FileInputStream fi = new FileInputStream(source);
BufferedInputStream sourceStream = new BufferedInputStream(fi,BUFFER);
TarArchiveEntry entry = new TarArchiveEntry(source, baseDir.getParentFile().toURI().relativize(source.toURI()).getPath());
int count;
byte data[] = new byte[BUFFER];
while ((count = sourceStream.read(data, 0, BUFFER)) != -1) {
out.write(data, 0, count);
}
sourceStream.close();
out.closeArchiveEntry();
} else {
if (source.listFiles() != null) {
if (source.listFiles().length == 0) {
System.out.println("Adding Empty Folder: "+ baseDir.toURI().relativize(source.toURI()).getPath());
TarArchiveEntry entry = new TarArchiveEntry(source, baseDir.getParentFile().toURI().relativize(source.toURI()).getPath());
out.putArchiveEntry(entry);
out.closeArchiveEntry();
}
for (File file : source.listFiles())
zipFilesRecursively(baseDir, file, out);
最佳答案
看这个:http://commons.apache.org/proper/commons-compress/tar.html#Long_File_Names
在使用流之前,您需要将格式设置为posix:
TarArchiveOutputStream stream = new TarArchiveOutputStream(...)
stream.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX)
关于java - 当我将文件tar压缩为 "is too long ( > 100 bytes) TarArchiveOutputStream"时,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32528799/