This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Problem with List of files to zipped Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problem with List of files to zipped " Watch "Problem with List of files to zipped " New topic
Author

Problem with List of files to zipped

sigamala viswanath
Greenhorn

Joined: Mar 13, 2006
Posts: 26
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

Joined: Apr 14, 2004
Posts: 10336


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

Can you show us what you tried?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
sigamala viswanath
Greenhorn

Joined: Mar 13, 2006
Posts: 26
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

Joined: Oct 27, 2005
Posts: 19654
    
  18

sigamala, could you please UseCodeTags? That will make reading your code so much easier.

You can use the edit button () to edit your post.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
sigamala viswanath
Greenhorn

Joined: Mar 13, 2006
Posts: 26
Problem resolved, Thanks.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19654
    
  18

Care to share the solution?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Problem with List of files to zipped
 
Similar Threads
Read subdirectories and count files?
how to extract rar or zip files in java?
prefix in XP
Moving file to specific folger
problem in unzipping