我正在使用ICE tar库来制作tar文件。
我的代码段是,
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import com.ice.tar.TarEntry;
import com.ice.tar.TarOutputStream;
public class Util {
public static void main(String[] args) {
tar();
}
public static void tar(){
try{
// Output file stream
FileOutputStream dest = new FileOutputStream( "f:/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("f:/test/myfile1.txt");
filesToTar[1]=new File("f:/test/myfile2.txt");
for(File f:filesToTar){
out.putNextEntry(new TarEntry(f));
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();
}catch(Exception e){
e.printStackTrace();
}
}
}
但是这仅适用于小尺寸文件。当我尝试tar大型文件时,我收到以下错误说明,
java.io.IOException: request to write '2048' bytes exceeds size in header of '1495040' bytes
at com.ice.tar.TarOutputStream.write(Unknown Source)
当我尝试更改
时 byte data[] = new byte[2048];
至byte data[] = new byte[origin.available()];
java.io.IOException: request to write '1495040' bytes exceeds size in header of '1495040' bytes
at com.ice.tar.TarOutputStream.write(Unknown Source)
我无法找到发生这种情况的原因。请帮忙。
修改 我尝试tar的大型文件本身就是一个tar文件。