🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.swing
Article #134

Re: zipping with java swing

#134
From: Rafael Amar
Date: Mon, 20 Feb 2006 09:00
144 lines
4751 bytes
I found the answer:
This will zip your dir and sub dirs.


import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.Deflater;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * @author tbryant
 *
 * To change the template for this generated type comment go to
 * Window>Preferences>Java>Code Generation>Code and
Comments
 */
public class FileZipper {
	private String[] filesToBeZipped;
	private String zipFileName;
	private static boolean verbose;
	private static  boolean usePath;
        public  File dirObj;

//	public FileZipper(String dir, String zipFileName) {
//  		this.zipFileName = zipFileName;
//		this.verbose = true;
//	}

	public static  void zip(String dir, String zipFileName){
            verbose = true;
             File dirObj = new File(dir);
              File[] f = dirObj.listFiles();
          //  File[] filesToBeZipped = dirObj.listFiles();
		ZipOutputStream zipOut = null;
		try {
			File zipFile = new File(zipFileName);
			if (zipFile.exists()) {
				zipFile.canWrite();
			}
			zipOut =
				new ZipOutputStream(
					new BufferedOutputStream(new FileOutputStream(zipFile)));
			zipOut.setLevel(Deflater.BEST_COMPRESSION);

			try {


				zipFiles(zipOut, f);
				zipOut.close();
			}
			catch (IOException e1) {
				e1.printStackTrace();
				return;
			}
		}
		catch (FileNotFoundException e) {
			e.printStackTrace();
			return;
		}
		if (verbose) {
			System.out.println(
				"\nZip file " + zipFileName + " has successfully been
created.");
		}
	}
	private static  void zipFiles(ZipOutputStream zipOut, File[] files)
		throws FileNotFoundException, IOException {

		File file = null;
		for (int i = 0; i < files.length; i++) {
			if (verbose) {
				System.out.println("Zipping file " + files[i]);
			}
			file = files[i];
			//if file is a directory, get child files and call this method
			if (file.isDirectory()) {
				File[] dirFiles = file.listFiles();
                                //  usePath=true;
				zipFiles(zipOut, dirFiles);
			}
			else {
				ZipEntry entry = null;

				BufferedInputStream in = null;
				byte[] buf = new byte[5120];

				in = new BufferedInputStream(new FileInputStream(file));
                              ;
				if (usePath) {
					entry = new ZipEntry(file.getAbsolutePath());
				}
				else {
                                       String pathToDir = "c:\\";

					entry = new
ZipEntry(file.getPath().substring(pathToDir.length()));
				}
				entry.setTime(file.lastModified());

				zipOut.putNextEntry(entry);

				int len;
				while ((len = in.read(buf)) > 0) {
					zipOut.write(buf, 0, len);
				}
				in.close();
				zipOut.closeEntry();
			}
		}
	}

	/**
	 * Used to set whether you want the path from the root of the file
system
	 * to the file you are zipping to be included in the zip archive.
	 *
	 * Normally this is not used.
	 *
	 * @param usePath boolean indicating whether to use path in archive
	 */
	public  void usePath(boolean usePath) {
		this.usePath = usePath;
	}

	public static void main(String[] args) {

		String[] files = null;

		//FileZipper fz = new FileZipper("c:\\PDF","C:\\moshe.zip");
	   zip("c:\\PDF","C:\\moshe.zip");
	}
}


thank you all for all the help......


Rafael Amar
Technical Details
Message-ID: <1140426050.308326.213610@f14g2000cwb.googlegroups.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!postnews.google.com!f14g2000cwb.googlegroups.com!not-for-mail
References: <1140082433.785934.21920@f14g2000cwb.googlegroups.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 111d45,a3a95f119fefd2d1
X-Google-Attributes: gid111d45,public
X-Google-Language: ENGLISH,ASCII-7-bit
Path: arc1-ca.pugleaf.net!g2news1.google.com!postnews.google.com!f14g2000cwb.googlegroups.com!not-for-mail
From: "Rafael Amar" <rafiamar@gmail.com>
Newsgroups: borland.public.jbuilder.java.swing
Subject: Re: zipping with java swing
Date: 20 Feb 2006 01:00:50 -0800
Organization: http://groups.google.com
Lines: 145
Message-ID: <1140426050.308326.213610@f14g2000cwb.googlegroups.com>
References: <1140082433.785934.21920@f14g2000cwb.googlegroups.com>
NNTP-Posting-Host: 147.235.190.2
Mime-Version: 1.0
Content-Type: text/plain; charset="iso-8859-1"
X-Trace: posting.google.com 1140426055 11118 127.0.0.1 (20 Feb 2006 09:00:55 GMT)
X-Complaints-To: groups-abuse@google.com
NNTP-Posting-Date: Mon, 20 Feb 2006 09:00:55 +0000 (UTC)
User-Agent: G2/0.2
X-HTTP-UserAgent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727),gzip(gfe),gzip(gfe)
X-HTTP-Via: 1.0 ISAPROXY
Complaints-To: groups-abuse@google.com
Injection-Info: f14g2000cwb.googlegroups.com; posting-host=147.235.190.2;
   posting-account=bkE1IA0AAABZ4seR5MXrNWck4qTjq9um