Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem with List of files to zipped

 
sigamala viswanath
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I had some issue with zip the files and folder together.

Problem is :

List filesList=new ArrayList();
filesList.add("c://test1.txt");
filesList.add("c://test1.txt");
ilesList.add("c://test");
Above i have List which have two files and one folder , Now i have to Zip these 3 files such way that i have to search "test" contains any folder again inside the folder we have any folder so on.


Can any body help me in getting the logic.
I tried the logic using recursive method but i could not succeeded.

Please help if you can get the solution.


Thanks,
Viswanath.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I tried the logic using recursive method but i could not succeeded

Can you show us what you tried?
 
sigamala viswanath
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public static void CreateZipFile(String ZipentryFile, String ZipDestFile,String path)
{
try {


FileUtilities f1= new FileUtilities();

ZipEntry zipAdd=null;
byte buffer[] = new byte[10240];
// Open archive file
File destfile= new File(ZipDestFile);
FileOutputStream stream = new FileOutputStream(destfile);
ZipOutputStream out = new ZipOutputStream(stream);
String sourcefileNamePath =null;
sourcefileNamePath = ZipentryFile;

File Sourcefile= new File(sourcefileNamePath);
System.out.println("Adding " + ZipentryFile);
// Add archive entry
if(Sourcefile.isDirectory())
{
for (String fileName : Sourcefile.list()) {

String Path= sourcefileNamePath+File.separator+fileName;
System.out.println("File name : "+Path);
File f111=new File(Path);

if(f111.isDirectory()){
//CreateZipFile1(Path, ZipDestFile,f1new.getName()+File.separator);
CreateZipFile(Path, ZipDestFile,f111.getName());

}
else{

zipAdd = new ZipEntry( path+Sourcefile.getName()+File.separator+f111.getName());
WritefiletoZip(out,Path,zipAdd);
}
}


}
else{
zipAdd = new ZipEntry(path+Sourcefile.getName());
WritefiletoZip(out,sourcefileNamePath,zipAdd);
}



out.close();
stream.close();
System.out.println("Adding completed OK");
} catch (Exception e) {
e.printStackTrace();
System.out.println("Error: " + e.getMessage());
return;
}

}
public static void WritefiletoZip(ZipOutputStream out,String streampath,ZipEntry zipAdd){
try{
byte buffer[] = new byte[10240];
//ZipEntry zipAdd=null;
out.putNextEntry(zipAdd);
FileInputStream in = new FileInputStream(streampath);
while (true) {
int nRead = in.read(buffer, 0, buffer.length);
if (nRead <= 0)
break;
out.write(buffer, 0, nRead);
}
in.close();
}catch(Exception e){

}
 
Rob Spoor
Sheriff
Pie
Posts: 20555
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sigamala, could you please UseCodeTags? That will make reading your code so much easier.

You can use the edit button () to edit your post.
 
sigamala viswanath
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Problem resolved, Thanks.
 
Rob Spoor
Sheriff
Pie
Posts: 20555
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Care to share the solution?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic