/**
* A simple command line utility to zip a folder and subfolder
*
* Use: java Zip folder zipfilename
* ex: java Zip test test.zip
*
*
* @Author : Bruno Augier
* @version 1.0, 21/07/2007
* @see: http://dzzd.net
*/
import java.util.zip.ZipOutputStream;
import java.util.zip.ZipEntry;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.File;
public class Zip
{
public static void main(String[] args)
{
try
{
String inputDir=args[0];
String outputFile=args[1];
ZipOutputStream zos = new
ZipOutputStream(new FileOutputStream(outputFile));
zipDirectory(inputDir, zos);
zos.close();
}
catch(Throwable t)
{
t.printStackTrace(System.out);
}
}
public static void zipDirectory(String dir2zip, ZipOutputStream zos) throws Throwable
{
File zipDir = new File(dir2zip);
String[] dirList = zipDir.list();
byte[] readBuffer = new byte[2156];
int bytesIn = 0;
for(int i=0; i<dirList.length; i++)
{
File f = new File(zipDir, dirList[i]);
if(f.isDirectory())
{
String filePath = f.getPath();
zipDirectory(filePath, zos);
continue;
}
FileInputStream fis = new FileInputStream(f);
ZipEntry anEntry = new ZipEntry(f.getPath());
zos.putNextEntry(anEntry);
while((bytesIn = fis.read(readBuffer)) != -1)
{
zos.write(readBuffer, 0, bytesIn);
}
fis.close();
}
}
}
This actually might help me out alot. A while ago, I tried to write a program for backing up my projects. It was supposed to create a zip file. For some reason, it was buggy. Examining your code my help me fix my problem.
I saved it to my desktop and will try fixing my code after I finish my current project.
Thanks for posting this code.
havent tried it, but how does this react to circular links in Linux? i remember that being a real problem for stuff i wrote that did recursife file traversal.
me too (I mean on linux OS)
if you give it a try let us know what the result!