сделать файл tar на Java
, если я использую выше кода, после прочтения строки TaroutputStream OUT = new TaroutputStream (новый BufferedOutputStream (dest)); Он просто выходит из метода (то есть после чтения этой строки ... он не выполнил код, который после этого. Последовательность непосредственно пошла в наконец -то блок, где назывался этот метод). < /p>
Для другого примера я использовал Apache Common Compress Jar. В этом случае он не выполнит сам класс Java, который выходит из класса формирования.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
Например, 2, я использовал apache.common.compress.1.2 в качестве банки.//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), my_tar_ball);
// Close Archieve entry, 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), my_tar_ball);
// Close Archieve entry, 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();
}
}
< /code>
Любой, пожалуйста, помогите решить эту проблему. Спасибо
Решение:
//Creating an array list
ArrayList fileList=null;
fileList = new ArrayList();
//Adding an File name with path in the array list
fileList.add();
FileOutputStream fos = null;
TarArchiveOutputStream tarOs = null;
//Tar the file
try {
fos = new FileOutputStream(fileNames);
tarOs = new TarArchiveOutputStream(fos);
for(int k=0; k < fileCountSize ;k++){
String filePath = fileList.get(k).toString();
File file = new File(filePath);
String entryName = file.getName();
tarOs.putArchiveEntry(new TarArchiveEntry(file, entryName));
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
IOUtils.copy(bis, tarOs);
tarOs.closeArchiveEntry();
fis.close();
bis.close();
}
} catch (IOException e) {
e.printStackTrace();
}finally{
if(tarOs != null){
tarOs.close();
}
if(fos != null){
fos.close();
}
}
Подробнее здесь: https://stackoverflow.com/questions/639 ... using-java