代码到c#中的zip文件夹 [英] code to zip folder in c#

查看:62
本文介绍了代码到c#中的zip文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在一个拉链中压缩entiry foder及其子文件夹和文件,我的意思是我要压缩一个孔文件夹?请帮帮我?



在他们的代码项目中有代码来压缩不在文件夹中的文件plz提供给代码zip文件夹

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

推荐答案

试试这个 http://dotnetzip.codeplex.com/ [ ^ ]


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
}



编辑:添加pre标签


added "pre" tags


我如何获得FileOutputStream和ZipOutputStream
how i get the FileOutputStream and ZipOutputStream


这篇关于代码到c#中的zip文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆