[root@VM-0-5-centos test]# tar -zxvf test.tar.gz tar: 它似乎不像是一个 tar 归档文件 tar: 跳转到下一个头
gzip: stdin: invalid compressed data--format violated tar: Child returned status 1 tar: Error is not recoverable: exiting now
使用的是官网提供的方法。做法是将一个文件夹里的文件打成 tar 包,然后再将 tar 压缩成 tar.gz
InputStream in = Files.newInputStream(Paths.get("archive.tar")); OutputStream fout = Files.newOutputStream(Paths.get("archive.tar.gz")); BufferedOutputStream out = new BufferedOutputStream(fout); GzipCompressorOutputStream gzOut = new GzipCompressorOutputStream(out); final byte[] buffer = new byte[buffersize]; int n = 0; while (-1 != (n = in.read(buffer))) { gzOut.write(buffer, 0, n); } gzOut.close(); in.close();