如何解决我想使用Java
我已经尝试了多个示例...但是非适用于我 Make tar file by Java 如果我使用上面的代码,则在读取行TaroutputStream out = new TaroutputStream(new bufferedoutputstream(dest));之后,它只是从方法中出来的(即,读完那行之后……它将不执行该代码之后的代码。Sequence直接进入该方法所调用的finally块)。
再举一个例子,我使用了Apache通用压缩Jar。在那种情况下,它不会执行java类本身,而是从FormBean类退出。
将从数据库中获取文件,文件将采用CSV格式,并且所有文件都将放置在“数组列表”中。
fileNames = "Security.smod."+SYSDATE;
strSavePath = D\:\\CMSREPORT\\reportZip\\
tarFunction(fileList,strSavePath,fileNames);
//Example function 1
private String tarFunction(ArrayList fileList,String strSavePath,String outFileName) {
int fileCountSize = fileList.size();
// Output file stream
FileOutputStream dest = new FileOutputStream( strSavePath );
// Create a TaroutputStream
TaroutputStream out = new TaroutputStream( new bufferedoutputstream( dest ) );
// Files to tar
File[] filesToTar=new File[3];
for(int i=0; i<fileCountSize; i++)
{
filesToTar[i]=new File((String) fileList.get(i);
}
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,count);
}
out.flush();
origin.close();
}
}
对于示例2,我将apache.common.compress.1.2用作jar。 在这种情况下,它将不执行java类,而是从FormBean类退出。
//Example function 2
private void tarFunc(){
try{
// Files to tar
File resource1 = new File((String) fileList.get(0));
File resource2 = new File((String) fileList.get(1));
File resource3 = new File((String) fileList.get(2));
// Output Stream - that will hold the physical TAR file
OutputStream tar_output = new FileOutputStream(new File(fileNames+".tar"));
// Create Archive Output Stream that attaches File Output Stream / and specifies TAR as type of compression
ArchiveOutputStream my_tar_ball = new ArchiveStreamFactory().createArchiveOutputStream(ArchiveStreamFactory.TAR,tar_output);
// Create Archieve entry - write header information
TararchiveEntry tar_file = new TararchiveEntry(resource1);
// length of the TAR file needs to be set using setSize method
tar_file.setSize(resource1.length());
my_tar_ball.putArchiveEntry(tar_file);
IoUtils.copy(new FileInputStream(resource1),my_tar_ball);
// Close Archieve entry,write trailer information
my_tar_ball.closeArchiveEntry();
// Repeat steps for the next file that needs to be added to the TAR
tar_file = new TararchiveEntry(resource2);
tar_file.setSize(resource2.length());
my_tar_ball.putArchiveEntry(tar_file);
IoUtils.copy(new FileInputStream(resource2),write trailer information
my_tar_ball.closeArchiveEntry();
tar_file = new TararchiveEntry(resource3);
tar_file.setSize(resource3.length());
my_tar_ball.putArchiveEntry(tar_file);
IoUtils.copy(new FileInputStream(resource3),write trailer information
my_tar_ball.closeArchiveEntry();
my_tar_ball.finish();
// Close output stream,our files are zipped
tar_output.close();
}catch(Exception e){
e.printstacktrace();
}
}
任何人都可以帮助解决此问题。谢谢:)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。