编程语言
首页 > 编程语言> > java-当我压缩文件时,其抛出异常为“过长(> 100字节)TarArchiveOutputStream”

java-当我压缩文件时,其抛出异常为“过长(> 100字节)TarArchiveOutputStream”

作者:互联网

当我压缩文件时,其抛出异常为“太长(> 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)

标签:apache-commons,java
来源: https://codeday.me/bug/20191027/1948105.html