Tuesday, September 23, 2008

ZipFiles

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.*;
public class ZipFiles
{
public static List getListOfAllFiles(String inFolder){
List listOfAllFiles = new ArrayList();

File f = new File(inFolder);
listOfAllFiles= makeFileList(listOfAllFiles, f);
return listOfAllFiles;

}
private static List makeFileList(List thisList, File f) {
if (f.exists() && f.isDirectory()){
File[] fileList =f.listFiles();
for (File file : fileList) {
if(file.exists() && file.isFile()){
thisList.add(file.getAbsolutePath());
}else if (file.exists() && file.isDirectory()) {
makeFileList(thisList,file);
}
}
}
return thisList;
}
public static void main(String a[])
{
String inFolderString = "E:\\Animation";
String files[] = getListOfAllFiles(inFolderString).toArray(new String[0]);
String outFileSting = "E:\\zip1\\myzip.zip";
String outFolderString = "E:\\zip1";

zipFiles(files,outFileSting,outFolderString);
}
public static void zipFiles(String[] files,String outFileSting,String outFolderString) {
try
{


File outFile=new File(outFileSting);
File outFolder = new File(outFolderString);

if(! outFolder.exists() || ! outFolder.isDirectory()){
outFolder.mkdirs();
}

ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(outFile)));
BufferedInputStream in = null;
byte[] data = new byte[1024];


for (int i=0; i {
in = new BufferedInputStream(new FileInputStream(files[i]), 1024);
out.putNextEntry(new ZipEntry(files[i]));
int count;
while((count = in.read(data,0,1024)) != -1)
{
out.write(data, 0, count);
}
out.closeEntry();
}
out.flush();
out.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}


No comments: