File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Java ZipFile Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Java ZipFile " Watch "Java ZipFile " New topic

Java ZipFile


Joined: Aug 04, 2004
Posts: 2
When I am running the zip code below, it zips only 3,108 files where as in my folder I've actuall 134,180 files. I am unable to figure out the reason why the code is not zipping all the files. Infact, code runs successfully without any errors but when I look into the zip file it has just 3,108 files only. Any idea? Any help would be highly appreciated.

Here is the code:
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------

import java.util.*;
import java.lang.*;
import java.lang.String.*;
import java.text.*;

public class NewCreateZipFile {

private static File dir;
private static String[] children;
private static String absPath= "C:\\QAData";

private static String dirName="Converted";

private static File outputLogFile;
private static String zipLogFile= absPath + "\\zipfile.log";

private static FileWriter outL;
private static double lStartTime;
private static double lEndTime;
private static double lTimeTaken;
private static double dTimeInSecs;
private static double dTimeInMins;
private static double dTimeInHours;

private static File zipDr;

private static String inputLocation = absPath + "\\" + dirName;
private static String outputLocation =absPath + "\\";

//here is the code for the method
public static void zipDir(String dir2zip, ZipOutputStream zos)
//create a new File object based on the directory we have to zip File
zipDr = new File(dir2zip);

//get a listing of the directory content

String[] dirList = zipDr.list();
byte[] readBuffer = new byte[10240];
int bytesIn = 0;

//loop through dirList, and zip the files

System.out.println("Total No. of Files to be archived: " + dirList.length);
outL.write("Total No. of Files to be archived: " + dirList.length);
int i;
for(i=0; i<dirList.length; i++)
File f = new File(zipDr, dirList[i]);
System.out.println("File Name: "+ f);
//if the File object is a directory, call this
//function again to add its content recursively
String filePath = f.getPath();
zipDir(filePath, zos);
//loop again
//if we reached here, the File object f was not a directory
//create a FileInputStream on top of f

FileInputStream fis = new FileInputStream(f);

//create a new zip entry
ZipEntry anEntry = new ZipEntry(f.getPath());

//place the zip entry in the ZipOutputStream object

//now write the content of the file to the ZipOutputStream
while((bytesIn = != -1)
zos.write(readBuffer, 0, bytesIn);

//close the Stream

int iTotalFiles =i-1;
System.out.println("Total Number of files zipped :" + iTotalFiles);
outL.write("Total Number of files zipped :" + iTotalFiles);
catch(Exception e)
{ //handle exception

public static void main(String[] args) throws IOException

lStartTime = System.currentTimeMillis();
outputLogFile = new File(zipLogFile);

outL= new FileWriter(outputLogFile);
//dir = new File(absPath, dirName);

//create a ZipOutputStream to zip the data to
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(outputLocation +"\\"+ dirName +".zip"));
zipDir(inputLocation, zos);
//close the stream
catch(Exception e)
//handle exception

lEndTime = System.currentTimeMillis();
lTimeTaken = lEndTime -lStartTime;
dTimeInSecs =lTimeTaken/1000.00;
dTimeInMins =dTimeInSecs/60.00;
dTimeInHours =dTimeInMins/60.00;

NumberFormat formatter = new DecimalFormat("#.#");
String s;

s = formatter.format(lTimeTaken);

System.out.println("\n Total time of execution:" + s + " milliseconds");
outL.write("\n Total time of execution: " + s + " milliseconds");

formatter = new DecimalFormat("#.000");
s = formatter.format(dTimeInSecs);

System.out.println("\n Total time of execution:" + s + " Seconds");
outL.write("\n Total time of execution: " + s + " Seconds");

formatter = new DecimalFormat("#.00000");
s = formatter.format(dTimeInMins);

System.out.println("\n Total time of execution:" + s + " Minutes");
outL.write("\n Total time of execution: " + s + " Minutes");

formatter = new DecimalFormat("#.000000");
s = formatter.format(dTimeInHours);

System.out.println("\n Total time of execution:" + s + " Hours");
outL.write("\n Total time of execution: " + s + " Hours");

Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
Java Neophyte,

Welcome to JavaRanch!

We ain't got many rules 'round these parts, but we do got one. Please change your display name to comply with The JavaRanch Naming Policy.

Thanks Pardner! Hope to see you 'round the Ranch!

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]

Joined: Aug 04, 2004
Posts: 2
I agree. Here's the link:
subject: Java ZipFile
It's not a secret anymore!