Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Updated 4-Apr-12 3:12am
v2

 
Share this answer
 
C#
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
 
Share this answer
 
v3
how i get the FileOutputStream and ZipOutputStream
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900