Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# GimmeCode
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 2:50am
sarita2535
Edited 4-Apr-12 4:12am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  
Rate this: bad
good
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
 

HTH!
  Permalink  
Rate this: bad
good
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}).MyZip.zip",
	DateTime.Now.ToString("yyyyMMdd")); 	//  Zip Destiny File Name
    string theDirectory = @"C:\FolderToZip";    	//  Folder to zip

    try
    {
        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
        {
            System.IO.File.Delete(ZipFileName);
            sb.Append(String.Format
	("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 + @"\", ""));
            zos.putNextEntry(ze);
 
            sbyte[] buffer = new sbyte[1024];
            int len;
 
            while ((len = fis.read(buffer)) >= 0)
            {
                zos.write(buffer, 0, len);  //  Write buffer to Zip File
            }
 
            fis.close();    //  Close input Stream
        }
 
        //  Close outputs
        zos.closeEntry();
        zos.close();
        fos.close();
 
        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
  Permalink  
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

how i get the FileOutputStream and ZipOutputStream
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 381
1 Sergey Alexandrovich Kryukov 265
2 Praneet Nadkar 237
3 Marcin Kozub 225
4 /\jmot 189
0 OriginalGriff 8,284
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,614
3 Maciej Los 4,989
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web04 | 2.8.1411023.1 | Last Updated 20 May 2013
Copyright © CodeProject, 1999-2014
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