Open() private method

private Open ( string archiveFileName, ZipArchiveMode mode ) : ZipArchive
archiveFileName string
mode ZipArchiveMode
return ZipArchive
Example #1
0
        public static void ZipTo(string archiveFilename, ZipFileInfo[] toArchiveFiles)
        {
            var zip = Zip.Open(archiveFilename, ZipArchiveMode.Create);

            foreach (var file in toArchiveFiles)
            {
                var filename = file.InArchiveFilename;
                if (String.IsNullOrEmpty(filename))
                {
                    filename = Path.GetFileName(file.SourceFilepath);
                }
                zip.CreateEntryFromFile(file.SourceFilepath, filename, CompressionLevel.Optimal);
            }
            // Dispose of the object when we are done
            zip.Dispose();
        }
Example #2
0
        /// <summary>
        /// 解压Zip文件
        /// </summary>
        /// <param name="zipFilePath"></param>
        /// <returns></returns>

        /*
         * public static ZipInfo UnZipFile(string zipFilePath)
         * {
         *  if (!File.Exists(zipFilePath))
         *  {
         *      return new ZipInfo
         *      {
         *          Success = false,
         *          InfoMessage = "没有找到解压文件"
         *      };
         *  }
         *  try
         *  {
         *      string path = zipFilePath.Replace(Path.GetExtension(zipFilePath), string.Empty) + "_" + System.DateTime.Now.ToString("yyyyMMddHHmmssfff");
         *      string directoryName = string.Empty;
         *      string fileName = string.Empty;
         *      using (ICSharpCode.SharpZipLib.Zip.ZipInputStream s = new ICSharpCode.SharpZipLib.Zip.ZipInputStream(File.OpenRead(zipFilePath)))
         *      {
         *          ICSharpCode.SharpZipLib.Zip.ZipEntry theEntry;
         *          while ((theEntry = s.GetNextEntry()) != null)
         *          {
         *              directoryName = Path.GetDirectoryName(theEntry.Name);
         *              fileName = Path.GetFileName(theEntry.Name);
         *
         *              // create directory
         *              if (directoryName.Length > 0)
         *              {
         *                  directoryName = Path.Combine(path, directoryName);
         *                  if (!Directory.Exists(directoryName))
         *                      Directory.CreateDirectory(directoryName);
         *              }
         *              else
         *              {
         *                  directoryName = path;
         *              }
         *
         *              if (fileName != String.Empty)
         *              {
         *                  fileName = Path.Combine(directoryName, fileName);
         *                  using (FileStream streamWriter = File.Create(fileName))
         *                  {
         *                      int size = 2048;
         *                      byte[] data = new byte[2048];
         *                      while (true)
         *                      {
         *                          size = s.Read(data, 0, data.Length);
         *                          if (size > 0)
         *                          {
         *                              streamWriter.Write(data, 0, size);
         *                          }
         *                          else
         *                          {
         *                              break;
         *                          }
         *                      }
         *                  }
         *              }
         *          }
         *      }
         *      return new ZipInfo
         *      {
         *          Success = true,
         *          InfoMessage = "解压成功",
         *          UnZipPath=path
         *      };
         *  }
         *  catch (Exception ex)
         *  {
         *      return new ZipInfo
         *      {
         *          Success = false,
         *          InfoMessage = "解压文件:" + ex.Message
         *      };
         *  }
         * }
         */
        public static ZipInfo UnZipFile(string zipFilePath)
        {
            string path          = zipFilePath.Replace(Path.GetExtension(zipFilePath), string.Empty) + "_" + System.DateTime.Now.ToString("yyyyMMddHHmmssfff");
            string directoryName = string.Empty;
            string fileName      = string.Empty;

            using (ZipArchive zip = ZipFile.Open(Path.Combine(zipFilePath), ZipArchiveMode.Read))
            {
                foreach (ZipArchiveEntry entry in zip.Entries)
                {
                    directoryName = Path.GetDirectoryName(entry.Name);

                    // create directory
                    if (directoryName.Length > 0)
                    {
                        directoryName = Path.Combine(path, directoryName);
                        if (!System.IO.Directory.Exists(directoryName))
                        {
                            System.IO.Directory.CreateDirectory(directoryName);
                        }
                    }
                    else
                    {
                        directoryName = path;
                    }


                    entry.ExtractToFile(path);
                }
            }

            if (!File.Exists(zipFilePath))
            {
                return(new ZipInfo
                {
                    Success = false,
                    InfoMessage = "没有找到解压文件"
                });
            }
            return(new ZipInfo
            {
                Success = true,
                InfoMessage = "解压成功",
                UnZipPath = path
            });
        }
Example #3
0
        public static void CreateFromDirectory(
            string sourceDirectoryName,
            string destinationArchiveFileName,
            CompressionLevel compressionLevel,
            bool includeBaseDirectory,
            Encoding entryNameEncoding)
        {
            if (sourceDirectoryName == null)
            {
                throw new ArgumentNullException("sourceDirectoryName");
            }

            if (destinationArchiveFileName == null)
            {
                throw new ArgumentNullException("destinationArchiveFileName");
            }

            if (string.IsNullOrWhiteSpace(sourceDirectoryName))
            {
                throw new ArgumentException("sourceDirectoryName");
            }

            if (string.IsNullOrWhiteSpace(destinationArchiveFileName))
            {
                throw new ArgumentException("destinationArchiveFileName");
            }

            if (entryNameEncoding == Encoding.Unicode ||
                entryNameEncoding == Encoding.UTF32 ||
                entryNameEncoding == Encoding.UTF7)
            {
                throw new ArgumentException("entryNameEncoding");
            }

            if (entryNameEncoding == null)
            {
                entryNameEncoding = Encoding.UTF8;
            }

            if (!Directory.Exists(sourceDirectoryName))
            {
                throw new DirectoryNotFoundException("sourceDirectoryName is invalid or does not exist");
            }

            var sourceDir = new DirectoryInfo(Path.GetFullPath(sourceDirectoryName));

            string fullBaseName = sourceDir.FullName;

            if (includeBaseDirectory && sourceDir.Parent != null)
            {
                fullBaseName = sourceDir.Parent.FullName;
            }

            bool hasEntries = false;

            char[] separators = new char[] {
                Path.DirectorySeparatorChar,
                Path.AltDirectorySeparatorChar
            };

            using (var zipFile = ZipFile.Open(destinationArchiveFileName, ZipArchiveMode.Create,
                                              entryNameEncoding)) {
                var entries = sourceDir.EnumerateFileSystemInfos("*", SearchOption.AllDirectories);
                foreach (var entry in entries)
                {
                    hasEntries = true;

                    int    length    = entry.FullName.Length - fullBaseName.Length;
                    string entryName = entry.FullName.Substring(fullBaseName.Length, length);

                    entryName = entryName.TrimStart(separators);

                    if (entry is FileInfo)
                    {
                        zipFile.CreateEntryFromFile(entry.FullName, entryName, compressionLevel);
                    }
                    else
                    {
                        zipFile.CreateEntry(entryName + Path.DirectorySeparatorChar);
                    }
                }

                // Create the base directory even if we had no entries
                if (includeBaseDirectory && !hasEntries)
                {
                    zipFile.CreateEntry(sourceDir.Name + Path.DirectorySeparatorChar);
                }
            }
        }
Example #4
0
 public static ZipArchive OpenRead(string archiveFileName)
 {
     return(ZipFile.Open(archiveFileName, ZipArchiveMode.Read));
 }
Example #5
0
 public static ZipArchive Open(ZipArchiveMode mode, string archiveFileName)
 {
     return(ZipFile.Open(archiveFileName, mode, null));
 }