当我为一个文件tar它的抛出异常为"太长(> 100字节)TarArchiveOutputStream"

时间:2015-09-11 17:15:19

标签: java apache-commons

当我将文件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);

1 个答案:

答案 0 :(得分:4)

看看这个:http://commons.apache.org/proper/commons-compress/tar.html#Long_File_Names

您需要在使用流之前将格式设置为posix:

TarArchiveOutputStream stream = new TarArchiveOutputStream(...)
stream.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX)