Click here to Skip to main content
11,805,035 members (61,156 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: C#
i want to zip entiry foder and its subfolder and files in one zip, i mean i wnt to zip a hole folder ? please help me?

in their code project have code to zip files not in folder plz provide to code zip folder
Posted 3-Apr-12 1:50am
Edited 4-Apr-12 3:12am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Rate this: bad
Please Sign up or sign in to vote.

Solution 2

In codeproject there is one utility posted. Find it useful.

C# Zip Files and/or Folders

Rate this: bad
Please Sign up or sign in to vote.

Solution 3

protected void btnZip_Click(object sender, EventArgs e)
    StringBuilder sb = new StringBuilder(); 	//  Builder to save report
    string ZipFileName = String.Format(@"C:\ZippedFolders\({0})",
	DateTime.Now.ToString("yyyyMMdd")); 	//  Zip Destiny File Name
    string theDirectory = @"C:\FolderToZip";    	//  Folder to zip

        sb.Append(String.Format("Directory To Zip: {0}.<br />", theDirectory));
        sb.Append(String.Format("Zip file: {0}.<br />", ZipFileName));
        string[] allFiles = Directory.GetFiles(theDirectory, "*.*",
	SearchOption.AllDirectories);   	// Get all files from
					// the folder to zip

        if (System.IO.File.Exists(ZipFileName)) 	//  Small piece of code
				// to delete zip file if it already exists
	("Deleted old Zip file: {0}.<br />", ZipFileName));
        //  J# code to zip

        FileOutputStream fos = new FileOutputStream(ZipFileName); //  J# output
						      // stream (Zip File)
        ZipOutputStream zos = new ZipOutputStream(fos);           //  J# output zip
        zos.setLevel(9);    //  Set the level of compression.
			// It may be a value between 0 and 9

            Add each file from folder to zip, to zip file.
            This way, the tree of the folder to zip will be
            reflected on the zip file
        for (int i = 0; i < allFiles.Length; i++ )
            string sourceFile = allFiles[i];
            FileInputStream fis = new FileInputStream(sourceFile);  //  J# input
						//stream to fill zip file
                Add the entry to the zip file (The Replace will remove the full path
                Ex.: file C:\FolderToZip\Files\Tmp\myFile.xml,
	  will be written as Files\Tmp\myFile.xml on the zip file
                If this code was not written, it would generate the
	  whole tree since the beginning of the FolderToZip
                This way the zip file begins directly at the contents
	  of C:\FolderToZip
            ZipEntry ze = new ZipEntry(sourceFile.Replace(theDirectory + @"\", ""));
            sbyte[] buffer = new sbyte[1024];
            int len;
            while ((len = >= 0)
                zos.write(buffer, 0, len);  //  Write buffer to Zip File
            fis.close();    //  Close input Stream
        //  Close outputs
        sb.Append(String.Format("Folder {0} Zipped successfully to File {1}.<br />",
					theDirectory, ZipFileName));
    catch (Exception eX)
        sb.Append(String.Format("Error zipping folder {0}. Details: {1}.
	Stack Trace: {2}.<br />", theDirectory, eX.Message, eX.StackTrace));
    lbReport.Text = sb.ToString();  //  Show result/report
Edit: added "pre" tags
Rate this: bad
Please Sign up or sign in to vote.

Solution 4

how i get the FileOutputStream and ZipOutputStream

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

  Print Answers RSS
0 OriginalGriff 315
1 CPallini 210
2 Richard Deeming 200
3 Sergey Alexandrovich Kryukov 168
4 CHill60 150
0 OriginalGriff 3,255
1 Maciej Los 1,990
2 KrunalRohit 1,907
3 CPallini 1,855
4 Richard MacCutchan 1,227

Advertise | Privacy | Mobile
Web01 | 2.8.151002.1 | Last Updated 20 May 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100