简体   繁体   中英

converting files in to single zip using C#

I am using C# code for aspx pages. I need to convert multiple files in to single zip file that can be readable by windows default zip software.

Can any one have an idea about it...?

See this tutorial: Creating Zip archives in .NET (without an external library like SharpZipLib)

ZipFile zip= new ZipFile("MyNewZip.zip"); 
zip.AddDirectory("My Pictures", true); // AddDirectory recurses subdirectories
zip.Save(); 

Or you can use SharpZipLib .

DotNetZip是一个很好的开源程序,没有任何许可问题。

//use this library SharpZipLib.
using this you can send multiple file for zipping which user selected and can save it to the physical path you specify either on client.

public string zipfile(string[] files)
    {


        string[] filenames = new string[files.Length];


            for (int i = 0; i < files.Length; i++)
                filenames[i] = HttpContext.Current.Request.PhysicalApplicationPath + files[i].Remove(0, 10// set it according to your filename).ToString();
        else
            for (int i = 0; i < files.Length; i++)
                filenames[i] = HttpContext.Current.Request.PhysicalApplicationPath + files[i].Replace(HttpContext.Current.Request.UrlReferrer.ToString(), "");
        string DirectoryName = filenames[0].Remove(filenames[0].LastIndexOf('/'));
        DirectoryName = DirectoryName.Substring(DirectoryName.LastIndexOf('/') + 1).Replace("\\", "");

        try
        {

            string newFile = HttpContext.Current.Request.PhysicalApplicationPath + "the physical path where you want to save it" + DirectoryName + ".zip";
            if (File.Exists(newFile))
                File.Delete(newFile);
            using (ZipFile zip = new ZipFile())
            {

                foreach (string file in filenames)
                {

                    string newfileName = file.Replace("\\'", "'");
                    zip.CompressionLevel = 0;
                    zip.AddFile(newfileName, "");
                }

                zip.Save(newFile);
            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM