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.
Thanks.
Here is the code:
// -----------------------------------------------------------------------------
// NewCreateZipFile.java
// -----------------------------------------------------------------------------
import java.util.zip.ZipOutputStream;
import java.util.zip.ZipEntry;
import java.io.*;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.File;
import java.io.FilenameFilter;
import java.util.*;
import java.lang.*;
import java.lang.String.*;
import java.io.FileFilter;
import java.text.*;
public class NewCreateZipFile {
private static File dir;
private static
String[] children;
private static String absPath= "C:\\QAData";
//"V:\\Technology\\DSS\\5500_students";
//"F:\\Documentum\\ReplaceFilename";
//"V:\\Technology\\DSS\\5500_students";
//"E:\\";
private static String dirName="Converted";
//"Converted";
//"test";
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)
{
try
{
//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(f.isDirectory())
{
//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
continue;
}
//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
zos.putNextEntry(anEntry);
//now write the content of the file to the ZipOutputStream
while((bytesIn = fis.read(readBuffer)) != -1)
{
zos.write(readBuffer, 0, bytesIn);
}
//close the Stream
fis.close();
}
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);
try{
//create a ZipOutputStream to zip the data to
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(outputLocation +"\\"+ dirName +".zip"));
zipDir(inputLocation, zos);
//close the stream
zos.close();
}
catch(Exception e)
{
//handle exception
System.out.println("Error");
}
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");
outL.close();
}
}