How to zip multiple files using only .net api in c

2019-01-11 09:42发布

I like to zip multiple files which are being created dynamically in my web application. Those files should be zipped. For this, i dont want to use any third-party tools. just like to use .net api in c#

9条回答
时光不老,我们不散
2楼-- · 2019-01-11 09:56

DotNetZip is the way to go (dotnetzip.codeplex.com)... don't try the .NET Packaging library.. too hard to use and the [Content_Types].xml that it puts in there bothers me..

查看更多
迷人小祖宗
3楼-- · 2019-01-11 09:58

With the release of the .NET Framework 4.5 this is actually a lot easier now with the updates to System.IO.Compression which adds the ZipFile class. There is a good walk-through on codeguru; however, the basics are in line with the following example:

using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.IO.Compression.FileSystem;

namespace ZipFileCreator
{
    public static class ZipFileCreator
    {
        /// <summary>
        /// Create a ZIP file of the files provided.
        /// </summary>
        /// <param name="fileName">The full path and name to store the ZIP file at.</param>
        /// <param name="files">The list of files to be added.</param>
        public static void CreateZipFile(string fileName, IEnumerable<string> files)
        {
            // Create and open a new ZIP file
            var zip = ZipFile.Open(fileName, ZipArchiveMode.Create);
            foreach (var file in files)
            {
                // Add the entry for each file
                zip.CreateEntryFromFile(file, Path.GetFileName(file), CompressionLevel.Optimal);
            }
            // Dispose of the object when we are done
            zip.Dispose();
        }
    }
}

查看更多
劳资没心,怎么记你
4楼-- · 2019-01-11 10:03

I'm not sure what you mean by not wanting to use thrid party tools, but I assume its that you don't want some nasty interop to programmatically do it through another piece of software.

I recommend using ICSharpCode SharpZipLib

This can be added to your project as a reference DLL and is fairly straightforward for creating ZIP files and reading them.

查看更多
仙女界的扛把子
5楼-- · 2019-01-11 10:10

http://www.codeplex.com/DotNetZip Source codes are available, so you can see how they do it and write something similiar for yourself

查看更多
Melony?
6楼-- · 2019-01-11 10:10

Check out System.IO.Compression.DeflateStream. Youll find a couple of examples on msdn http://msdn.microsoft.com/en-us/library/system.io.compression.deflatestream.aspx

查看更多
登录 后发表回答