Ionic.Zip.ZipFile.AddDirectory C# (CSharp) Method

AddDirectory() public method

Adds the contents of a filesystem directory to a Zip file archive.

The name of the directory may be a relative path or a fully-qualified path. Any files within the named directory are added to the archive. Any subdirectories within the named directory are also added to the archive, recursively.

Top-level entries in the named directory will appear as top-level entries in the zip archive. Entries in subdirectories in the named directory will result in entries in subdirectories in the zip archive.

If you want the entries to appear in a containing directory in the zip archive itself, then you should call the AddDirectory() overload that allows you to explicitly specify a directory path for use in the archive.

For ZipFile properties including Encryption, , SetCompression, , ExtractExistingFile, ZipErrorAction, and CompressionLevel, their respective values at the time of this call will be applied to each ZipEntry added.

public AddDirectory ( string directoryName ) : ZipEntry
directoryName string The name of the directory to add.
return ZipEntry
        public ZipEntry AddDirectory(string directoryName)
        {
            return AddDirectory(directoryName, null);
        }

Same methods

ZipFile::AddDirectory ( string directoryName, string directoryPathInArchive ) : ZipEntry

Usage Example

Ejemplo n.º 1
0
 /// <summary>
 /// 压缩ZIP文件
 /// </summary>
 /// <param name="fileList">待压缩的文件或目录集合</param>
 /// <param name="zipName">压缩后的文件名</param>
 /// <param name="isDirStruct">是否按目录结构压缩</param>
 public static bool Compress(List<string> fileList, string zipName, bool isDirStruct)
 {
     try
     {
         using (var zip = new ZipFile(Encoding.Default))
         {
             foreach (string path in fileList)
             {
                 string fileName = Path.GetFileName(path);
                 if (Directory.Exists(path))
                 {
                     //按目录结构压缩
                     if (isDirStruct)
                     {
                         zip.AddDirectory(path, fileName);
                     }
                     else//目录下文件压缩到根目录
                     {
                         zip.AddDirectory(path);
                     }
                 }
                 if (File.Exists(path))
                 {
                     zip.AddFile(path);
                 }
             }
             zip.Save(zipName);
             return true;
         }
     }
     catch (Exception)
     {
         return false;
     }
 }
All Usage Examples Of Ionic.Zip.ZipFile::AddDirectory