🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.jbuilder.java.swing
2 messages
2 total messages Started by Rafael Amar Thu, 16 Feb 2006 09:33
zipping with java swing
#133
Author: Rafael Amar
Date: Thu, 16 Feb 2006 09:33
4 lines
1198 bytes
Is there a way to zip a diractory with java swing?

Thanks
Rafael Amar
Re: zipping with java swing
#134
Author: 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
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads