Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Progress of zipping of folder on ProgressMonitor??  RSS feed

 
Bikash Paul
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all friends,
I am facing problem with showing the progress of zipping of folder for that Iam using ProgressMonitor.I am really confused what will be the code for that I have tried with my below codes but couldn't slove my problem.My below codes zipping folder successfully only problem with showing the progress of zipping on ProgressMonitor.Can any one please test my code where Iam wrong.
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.zip.*;
public class ZipUtility1 {
ZipOutputStream cpZipOutputStream = null;
String strSource = "";
String strTarget = "";
String strSubstring = "";
public static void main(String[] args) {
if(args == null || args.length < 2) {
System.out.println("Usage: java ZipUtility <directory or file to be zipped> <name of zip file to be created>");
return;
}
ZipUtility1 udZipUtility = new ZipUtility1();
udZipUtility.strSource = args[0];
udZipUtility.strTarget = args[1];
udZipUtility.zip();
}
private void zip(){
try{
File cpFile = new File(strSource);
if (!cpFile.isFile() && !cpFile.isDirectory() ){
System.out.println("\nSource file/directory Not Found!");
return;
}
if (cpFile.isDirectory()) {
strSubstring = strSource;
}
else {
strSubstring= "";
}
cpZipOutputStream = new ZipOutputStream(new FileOutputStream(strTarget));
cpZipOutputStream.setLevel(9);
zipFiles(cpFile,null);
cpZipOutputStream.finish();
cpZipOutputStream.close();
JOptionPane.showMessageDialog( null, "ZIPING COMPLETE");
System.out.println("\n Finished creating zip file " + strTarget + " from source" + strSource);
}catch (Exception e){
e.printStackTrace();
}
private void zipFiles(File cpFile,Component parentComponent){
int size=(int)lengthOfDirectory(cpFile);
ProgressMonitor monitor = new ProgressMonitor(parentComponent,"Packing " + strSource + "...", "",0,size);
monitor.setMillisToDecideToPopup(0);
monitor.setMillisToPopup(0);
if (cpFile.isDirectory()) {
File fList[] = cpFile.listFiles();
for (int i=0; i< fList.length; i++){
zipFiles(fList[i],null);
}
}
else {
try {
String strAbsPath = cpFile.getAbsolutePath();
String strZipEntryName ="";
if (!strSubstring.equals("") ){
strZipEntryName = strAbsPath.substring(strSource.length()+1,strAbsPath.length());
}
else {
strZipEntryName= cpFile.getName();
}
byte[] buf = new byte[16384];
FileInputStream cpFileInputStream = new FileInputStream(cpFile);
ZipEntry cpZipEntry = new ZipEntry(strZipEntryName);
cpZipOutputStream.putNextEntry(cpZipEntry);
monitor.setNote(cpZipEntry.getName());
int len;
int count=0;
while ((len = cpFileInputStream.read(buf))>0) {
cpZipOutputStream.write(buf,0,len);
count+=len;
monitor.setProgress(count);
}
cpZipOutputStream.closeEntry() ;
catch (Exception e) {
e.printStackTrace();
}
}
}
public static long lengthOfDirectory(File directory)
{
long count = 0;
File[] f = directory.listFiles();
if (f == null) return 0;
for (int i = 0, i < f.length; i++)
if (f[i].isDirectory())
count += lengthOfDirectory(f[i]);
else
count += f[i].length();
return count;
}
}
Regards
Bikash
[ July 12, 2003: Message edited by: Bikash Paul ]
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, you don't tell us what you expect to happen, and what happened instead, but I'll offer you one suggestion: the routine zipFiles() creates a ProgressMonitor and bumps it along, but it also calls itself recursively to handle directories full of files. I believe that all ProgressMonitors share the same dialog, and each time you create a new one, that dialog gets reinitialized. Therefore, this recursive routine is going to pop up a progress monitor, then continually reset the progress to 0.
You need to split zipFiles() into several routines, one called zipFiles() that is the top-level control routine and creates a ProgressMonitor; this can optionally call zipDirectory() or zipOneFile(). zipDirectory() calls zipOneFile() (and itself, recursively). The single ProgressMonitor gets passed as an argument.
 
Bikash Paul
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I want to show the progress of zipping of folder on ProgressMonitor means when my zipFiles() start zipping a folder I want to show the status of zipping on ProgressMonitor means i want to update my ProgressMonitor how much zipping of folder has been completed.Can you please give me some code how I can split my zipFiles() into several routines.Eagerly waiting for ur reply.
Thanks & Regards
Bikash
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!