我想使用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 @R_44_4045@ion
        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 @R_44_4045@ion 
        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 @R_44_4045@ion 
        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 @R_44_4045@ion 
        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 (将#修改为@)