我想使用Java以编程方式将文件夹压缩为tar文件。我认为一定有开源或库可以做到这一点,但我找不到这样的方法。
另外,我能否制作一个zip文件,然后将其扩展名更改为.tar?
是否有人可以建议一个库来完成这个任务?谢谢!
我想使用Java以编程方式将文件夹压缩为tar文件。我认为一定有开源或库可以做到这一点,但我找不到这样的方法。
另外,我能否制作一个zip文件,然后将其扩展名更改为.tar?
是否有人可以建议一个库来完成这个任务?谢谢!
我会看一下Apache Commons Compress。
在此示例页面的中间部分,有一个展示tar示例的例子。
TarArchiveEntry entry = new TarArchiveEntry(name);
entry.setSize(size);
tarOutput.putArchiveEntry(entry);
tarOutput.write(contentOfEntry);
tarOutput.closeArchiveEntry();
您可以使用 jtar - Java Tar库。
引用自他们的网站:
JTar是一个简单的Java Tar库,提供了一种使用IO流创建和读取tar文件的简便方式。该API非常易于使用,并类似于java.util.zip包。
以下为其网站提供的示例:
// Output file stream
FileOutputStream dest = new FileOutputStream( "c:/test/test.tar" );
// Create a TarOutputStream
TarOutputStream out = new TarOutputStream( new BufferedOutputStream( dest ) );
// Files to tar
File[] filesToTar=new File[2];
filesToTar[0]=new File("c:/test/myfile1.txt");
filesToTar[1]=new File("c:/test/myfile2.txt");
for(File f:filesToTar){
out.putNextEntry(new TarEntry(f, f.getName()));
BufferedInputStream origin = new BufferedInputStream(new FileInputStream( f ));
int count;
byte data[] = new byte[2048];
while((count = origin.read(data)) != -1) {
out.write(data, 0, count);
}
out.flush();
origin.close();
}
out.close();
.tar
归档文件并没有压缩。你需要对其进行文件压缩,例如使用 gzip 将其转换为 .tar.gz
。
如果你只是想归档一个目录,请看一下以下内容:
我已经编写了以下代码来解决这个问题。这段代码会检查要合并的文件是否已经存在于tar文件中,并更新该条目。如果不存在,则将其附加到存档结尾。
import org.apache.commons.compress.archivers.ArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
public class TarUpdater {
private static final int buffersize = 8048;
public static void updateFile(File tarFile, File[] flist) throws IOException {
// get a temp file
File tempFile = File.createTempFile(tarFile.getName(), null);
// delete it, otherwise you cannot rename your existing tar to it.
if (tempFile.exists()) {
tempFile.delete();
}
if (!tarFile.exists()) {
tarFile.createNewFile();
}
boolean renameOk = tarFile.renameTo(tempFile);
if (!renameOk) {
throw new RuntimeException(
"could not rename the file " + tarFile.getAbsolutePath() + " to " + tempFile.getAbsolutePath());
}
byte[] buf = new byte[buffersize];
TarArchiveInputStream tin = new TarArchiveInputStream(new FileInputStream(tempFile));
OutputStream outputStream = new BufferedOutputStream(Files.newOutputStream(tarFile.toPath()));
TarArchiveOutputStream tos = new TarArchiveOutputStream(outputStream);
tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
//read from previous version of tar file
ArchiveEntry entry = tin.getNextEntry();
while (entry != null) {//previous file have entries
String name = entry.getName();
boolean notInFiles = true;
for (File f : flist) {
if (f.getName().equals(name)) {
notInFiles = false;
break;
}
}
if (notInFiles) {
// Add TAR entry to output stream.
if (!entry.isDirectory()) {
tos.putArchiveEntry(new TarArchiveEntry(name));
// Transfer bytes from the TAR file to the output file
int len;
while ((len = tin.read(buf)) > 0) {
tos.write(buf, 0, len);
}
}
}
entry = tin.getNextEntry();
}
// Close the streams
tin.close();//finished reading existing entries
// Compress new files
for (int i = 0; i < flist.length; i++) {
if (flist[i].isDirectory()) {
continue;
}
InputStream fis = new FileInputStream(flist[i]);
TarArchiveEntry te = new TarArchiveEntry(flist[i],flist[i].getName());
//te.setSize(flist[i].length());
tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
tos.setBigNumberMode(2);
tos.putArchiveEntry(te); // Add TAR entry to output stream.
// Transfer bytes from the file to the TAR file
int count = 0;
while ((count = fis.read(buf, 0, buffersize)) != -1) {
tos.write(buf, 0, count);
}
tos.closeArchiveEntry();
fis.close();
}
// Complete the TAR file
tos.close();
tempFile.delete();
}
}
compile group: 'org.apache.commons', name: 'commons-compress', version: '1.+'
我还试过使用org.xeustechnologies:jtar:1.1,但性能远低于org.apache.commons:commons-compress:1.12提供的。
不同实现的性能笔记:
使用Java 1.8 zip压缩10次:
- java.util.zip.ZipEntry;
- java.util.zip.ZipInputStream;
- java.util.zip.ZipOutputStream;
[2016-07-19 19:13:11] 开始
[2016-07-19 19:13:18] 完成
7秒
使用jtar进行10次打包:
- org.xeustechnologies.jtar.TarEntry;
- org.xeustechnologies.jtar.TarInputStream;
- org.xeustechnologies.jtar.TarOutputStream;
[2016-07-19 19:21:23] 开始
[2016-07-19 19:25:18] 完成
3分55秒
调用Cygwin /usr/bin/tar进行10次打包:
[2016-07-19 19:33:04] 开始
[2016-07-19 19:33:14] 完成
14秒
使用org.apache.commons.compress进行100次打包:
- org.apache.commons.compress.archivers.ArchiveEntry;
- org.apache.commons.compress.archivers.tar.TarArchiveEntry;
- org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
- org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
[2016-07-19 23:04:45] 开始
[2016-07-19 23:04:48] 完成
3秒
使用org.apache.commons.compress进行1000次打包:
[2016-07-19 23:10:28] 开始
[2016-07-19 23:10:48] 完成
20秒