Click here to Skip to main content
Click here to Skip to main content
Articles » Languages » C# » General » Downloads
 
Add your own
alternative version

Zip and Unzip from a C# program using J# runtime

, 25 Apr 2004 CPOL
Zip and Unzip from a C# program using java.util.zip
easyzipunzip_demo.zip
App.ico
EasyZipUnZip.csproj.user
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;

namespace EasyZipUnZip
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.MainMenu mainMenu1;
		private System.Windows.Forms.ListView listView1;
		private System.Windows.Forms.MenuItem menuItem1;
		private System.Windows.Forms.MenuItem menuItem6;
		private System.Windows.Forms.MenuItem menuItem7;
		private System.Windows.Forms.MenuItem miOpen;
		private System.Windows.Forms.MenuItem miSave;
		private System.Windows.Forms.MenuItem miNew;
		private System.Windows.Forms.MenuItem miExit;
		private System.Windows.Forms.MenuItem miAdd;
		private System.Windows.Forms.MenuItem miExtract;
		private System.Windows.Forms.ColumnHeader columnHeader1;
		private System.Windows.Forms.ColumnHeader columnHeader2;
		private System.Windows.Forms.OpenFileDialog openFileDialog1;
		private System.Windows.Forms.SaveFileDialog saveFileDialog1;
		private System.Windows.Forms.StatusBar statusBar1;
		private System.Windows.Forms.MenuItem miRemove;
		private bool newFile = true;
		private string zipFileName = "";
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.mainMenu1 = new System.Windows.Forms.MainMenu();
			this.menuItem1 = new System.Windows.Forms.MenuItem();
			this.miNew = new System.Windows.Forms.MenuItem();
			this.miOpen = new System.Windows.Forms.MenuItem();
			this.miSave = new System.Windows.Forms.MenuItem();
			this.menuItem6 = new System.Windows.Forms.MenuItem();
			this.miExit = new System.Windows.Forms.MenuItem();
			this.menuItem7 = new System.Windows.Forms.MenuItem();
			this.miAdd = new System.Windows.Forms.MenuItem();
			this.miRemove = new System.Windows.Forms.MenuItem();
			this.miExtract = new System.Windows.Forms.MenuItem();
			this.listView1 = new System.Windows.Forms.ListView();
			this.columnHeader1 = new System.Windows.Forms.ColumnHeader();
			this.columnHeader2 = new System.Windows.Forms.ColumnHeader();
			this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
			this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
			this.statusBar1 = new System.Windows.Forms.StatusBar();
			this.SuspendLayout();
			// 
			// mainMenu1
			// 
			this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					  this.menuItem1,
																					  this.menuItem7});
			// 
			// menuItem1
			// 
			this.menuItem1.Index = 0;
			this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					  this.miNew,
																					  this.miOpen,
																					  this.miSave,
																					  this.menuItem6,
																					  this.miExit});
			this.menuItem1.Text = "&File";
			// 
			// miNew
			// 
			this.miNew.Index = 0;
			this.miNew.Text = "&New";
			this.miNew.Click += new System.EventHandler(this.miNew_Click);
			// 
			// miOpen
			// 
			this.miOpen.Index = 1;
			this.miOpen.Text = "&Open";
			this.miOpen.Click += new System.EventHandler(this.miOpen_Click);
			// 
			// miSave
			// 
			this.miSave.Enabled = false;
			this.miSave.Index = 2;
			this.miSave.Text = "&Save";
			this.miSave.Click += new System.EventHandler(this.miSave_Click);
			// 
			// menuItem6
			// 
			this.menuItem6.Index = 3;
			this.menuItem6.Text = "-";
			// 
			// miExit
			// 
			this.miExit.Index = 4;
			this.miExit.Text = "&Exit";
			this.miExit.Click += new System.EventHandler(this.miExit_Click);
			// 
			// menuItem7
			// 
			this.menuItem7.Index = 1;
			this.menuItem7.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					  this.miAdd,
																					  this.miRemove,
																					  this.miExtract});
			this.menuItem7.Text = "&Edit";
			// 
			// miAdd
			// 
			this.miAdd.Index = 0;
			this.miAdd.Text = "&Add";
			this.miAdd.Click += new System.EventHandler(this.miAdd_Click);
			// 
			// miRemove
			// 
			this.miRemove.Enabled = false;
			this.miRemove.Index = 1;
			this.miRemove.Text = "&Remove";
			this.miRemove.Click += new System.EventHandler(this.miRemove_Click);
			// 
			// miExtract
			// 
			this.miExtract.Enabled = false;
			this.miExtract.Index = 2;
			this.miExtract.Text = "&Extract";
			this.miExtract.Click += new System.EventHandler(this.miExtract_Click);
			// 
			// listView1
			// 
			this.listView1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
				| System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
																						this.columnHeader1,
																						this.columnHeader2});
			this.listView1.FullRowSelect = true;
			this.listView1.GridLines = true;
			this.listView1.Location = new System.Drawing.Point(0, 0);
			this.listView1.Name = "listView1";
			this.listView1.Size = new System.Drawing.Size(308, 240);
			this.listView1.TabIndex = 0;
			this.listView1.View = System.Windows.Forms.View.Details;
			this.listView1.SelectedIndexChanged += new System.EventHandler(this.listView1_SelectedIndexChanged);
			// 
			// columnHeader1
			// 
			this.columnHeader1.Text = "File Name";
			this.columnHeader1.Width = 200;
			// 
			// columnHeader2
			// 
			this.columnHeader2.Text = "Size";
			this.columnHeader2.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
			this.columnHeader2.Width = 100;
			// 
			// saveFileDialog1
			// 
			this.saveFileDialog1.DefaultExt = "zip";
			this.saveFileDialog1.Filter = "Zip files|*.zip";
			// 
			// statusBar1
			// 
			this.statusBar1.Location = new System.Drawing.Point(0, 244);
			this.statusBar1.Name = "statusBar1";
			this.statusBar1.Size = new System.Drawing.Size(312, 22);
			this.statusBar1.TabIndex = 1;
			this.statusBar1.Text = "Ready";
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(312, 266);
			this.Controls.Add(this.statusBar1);
			this.Controls.Add(this.listView1);
			this.Menu = this.mainMenu1;
			this.Name = "Form1";
			this.Text = "Untitled - Easy Zip/UnZip";
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}
		/// <summary>
		/// Disable remove files from zip
		/// Disable extract from new zip
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void listView1_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			miRemove.Enabled = (listView1.SelectedItems.Count > 0) && newFile;
			miExtract.Enabled = (listView1.SelectedItems.Count > 0) && (!newFile);
		}

		private void miExit_Click(object sender, System.EventArgs e)
		{
			Close();
		}
		/// <summary>
		/// Searching file name in listview
		/// </summary>
		/// <param name="fileName"></param>
		/// <returns>true if found</returns>
		private bool foundInList(string fileName)
		{
			for (int i = 0; i < listView1.Items.Count; i++)
			{
				if (fileName == listView1.Items[i].Text) return true;
			}
			return false;
		}
		/// <summary>
		/// Add file name to listView
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void miAdd_Click(object sender, System.EventArgs e)
		{
			if (openFileDialog1.ShowDialog() == DialogResult.OK)
			{
				string fileName = openFileDialog1.FileName;
				if (!foundInList(fileName))
				{
					ListViewItem fileItem = listView1.Items.Add(fileName);
					FileInfo fi = new FileInfo(fileName);
					fileItem.SubItems.Add(fi.Length.ToString());
					miSave.Enabled = true;
				}
				else
					MessageBox.Show("You can't add again this file!", Text);
			}
		}
		/// <summary>
		/// Write zip
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void miSave_Click(object sender, System.EventArgs e)
		{
			if (saveFileDialog1.ShowDialog() == DialogResult.OK)
			{
				zipFileName = saveFileDialog1.FileName;
				java.io.FileOutputStream fos = new java.io.FileOutputStream(zipFileName);
				java.util.zip.ZipOutputStream zos = new java.util.zip.ZipOutputStream(fos);
				for (int i = 0; i < listView1.Items.Count; i++)
				{
					string sourceFile = listView1.Items[i].Text;
					statusBar1.Text = sourceFile;
					java.io.FileInputStream fis = new java.io.FileInputStream(sourceFile);
					// File name format in zip file is:
					// folder/subfolder/filename
					// Let's delete drive name and replace '\' with '/':
					java.util.zip.ZipEntry ze = new java.util.zip.ZipEntry(sourceFile.Substring(3).Replace('\\','/'));
					zos.putNextEntry(ze);
					sbyte[] buffer = new sbyte[1024];
					int len;
					while((len = fis.read(buffer)) >= 0)
					{
						zos.write(buffer, 0, len);
					}
					zos.closeEntry();
					fis.close();
				}
				zos.close();
				fos.close();
				statusBar1.Text = "Ready";
				Text = zipFileName + " - Easy Zip/UnZip";
				newFile = false;
			}
		}
		/// <summary>
		/// Open zip file
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void miOpen_Click(object sender, System.EventArgs e)
		{
			if (openFileDialog1.ShowDialog() == DialogResult.OK)
			{
				listView1.Items.Clear();
				statusBar1.Text = "Reading...";
				zipFileName = openFileDialog1.FileName;
				java.io.FileInputStream fis = new java.io.FileInputStream(zipFileName);
				java.util.zip.ZipInputStream zis = new java.util.zip.ZipInputStream(fis);
				java.util.zip.ZipEntry ze;
				while ((ze = zis.getNextEntry()) != null)
				{
					if (!ze.isDirectory())
					{
						ListViewItem fileItem = listView1.Items.Add(ze.getName());
						fileItem.SubItems.Add(ze.getSize().ToString());
					}
				}
				zis.close();
				fis.close();
				statusBar1.Text = "Ready";
				Text = zipFileName + " - Easy Zip/UnZip";
				newFile = false;
				miAdd.Enabled = false;
			}
		}
		/// <summary>
		/// New zip
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void miNew_Click(object sender, System.EventArgs e)
		{
			listView1.Items.Clear();
			Text = "Untitled - Easy Zip/UnZip";
			newFile = true;
			miRemove.Enabled = false;
			miExtract.Enabled = false;
			miAdd.Enabled = true;
			zipFileName = "";
		}
		/// <summary>
		/// Extract selected files from zip
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void miExtract_Click(object sender, System.EventArgs e)
		{
			sbyte[] buf = new sbyte[1024];
			int len;
			for (int i = 0; i < listView1.SelectedItems.Count; i++)
			{
				string fileName = listView1.SelectedItems[i].Text;
				java.io.FileInputStream fis = new java.io.FileInputStream(zipFileName);
				java.util.zip.ZipInputStream zis = new java.util.zip.ZipInputStream(fis);
				java.util.zip.ZipEntry ze;
				while ((ze = zis.getNextEntry()) != null)
				{
					if (fileName == ze.getName())
					{
						// File name format in zip file is:
						// folder/subfolder/filename
						// Let's check...
						int index = fileName.LastIndexOf('/');
						if (index > 1)
						{
							string folder = fileName.Substring(0, index);
							DirectoryInfo di = new DirectoryInfo(folder);
							// Create directory if not exists
							if (!di.Exists)
								di.Create();
						}
						java.io.FileOutputStream fos = new java.io.FileOutputStream(fileName);
						while ((len = zis.read(buf)) >= 0)
						{
							fos.write(buf, 0, len);
						}
						fos.close();
						statusBar1.Text = fileName;
					}
				}
				zis.close();
				fis.close();
			}
			statusBar1.Text = "Ready";
		}
		/// <summary>
		/// Remove selected file names from listView
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void miRemove_Click(object sender, System.EventArgs e)
		{
			for (int i = listView1.Items.Count-1; i >= 0; i--)
			{
				if (listView1.Items[i].Selected)
					listView1.Items.RemoveAt(i);
			}
			miSave.Enabled = (listView1.Items.Count > 0);
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Valeri Makarov
Software Developer
Russian Federation Russian Federation
Over 12 years of experience as a Software Developer/Engineer, development and production support applications for MS DOS, MS Windows.
Certified C Programmer
Certified C++ Programmer
Certified Visual FoxPro Programmer
Certified C# Programmer

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141220.1 | Last Updated 26 Apr 2004
Article Copyright 2004 by Valeri Makarov
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid