Exemplo n.º 1
0
        /// <summary>
        /// Removes one of many files in storage. It creates a new Zip file.
        /// </summary>
        /// <param name="_zip">Reference to the current Zip object</param>
        /// <param name="_zfes">List of Entries to remove from storage</param>
        /// <returns>True if success, false if not</returns>
        /// <remarks>This method only works for storage of type FileStream</remarks>
        public static bool RemoveEntries(ref ZipStorer _zip, List <ZipFileEntry> _zfes)
        {
            if (!(_zip.ZipFileStream is FileStream))
            {
                throw new InvalidOperationException("RemoveEntries is allowed just over streams of type FileStream");
            }

            //Get full list of entries
            List <ZipFileEntry> fullList = _zip.ReadCentralDir();
            //In order to delete we need to create a copy of the zip file excluding the selected items
            string tempZipName   = Path.GetTempFileName();
            string tempEntryFile = Path.GetTempFileName();

            try
            {
                ZipStorer tempTargetZip = ZipStorer.Create(tempZipName, _zip.Comment);
                foreach (ZipFileEntry zfe in fullList)
                {
                    if (!_zfes.Contains(zfe))
                    {
                        if (_zip.ExtractFile(zfe, tempEntryFile))
                        {
                            tempTargetZip.AddFile(zfe.Method, tempEntryFile, zfe.FilenameInZip, zfe.Comment);
                        }
                    }
                }
                _zip.Close();
                tempTargetZip.Close();

                File.Delete(_zip.FileName);
                File.Move(tempZipName, _zip.FileName);
                _zip = ZipStorer.Open(_zip.FileName, _zip.Access);
            }
            catch
            {
                return(false);
            }
            finally
            {
                if (File.Exists(tempZipName))
                {
                    File.Delete(tempZipName);
                }
                if (File.Exists(tempEntryFile))
                {
                    File.Delete(tempEntryFile);
                }
            }
            return(true);
        }
Exemplo n.º 2
0
 public static void ExtractZipToDir(string zipFilePath, string extraDir)
 {
     using (ZipStorer zip = Open(zipFilePath, FileAccess.Read))
     {
         if (!Directory.Exists(extraDir))
         {
             Directory.CreateDirectory(extraDir);
         }
         zip.ReadCentralDirAction(delegate(ZipFileEntry f)
         {
             zip.ExtractFile(f, Path.Combine(extraDir, f.FilenameInZip));
         });
     }
 }
Exemplo n.º 3
0
        /// <summary>
        /// 释放包内文件到当前目录下并保留覆盖文件的文件备份
        /// </summary>
        /// <param name="zipFilePath">要释放的zip包文件</param>
        /// <param name="extraDir">释放目录</param>
        /// <param name="overrideFpkgPath">覆盖文件备份包名称,为空或不传值则自动命名</param>
        public static void ExtractZipToDirWithOverrideBak(string zipFilePath, string extraDir, string overrideFpkgPath)
        {
            if (string.IsNullOrEmpty(overrideFpkgPath))
            {
                int idx = zipFilePath.IndexOf('.');
                overrideFpkgPath = zipFilePath.Substring(0, idx) + "_overridebak.zip";
            }

            MemoryStream ms = new MemoryStream();
            StreamWriter sw = new StreamWriter(ms);
            int          successCount = 0, failedBakCount = 0, overrideCount = 0;

            using (ZipStorer zipBak = ZipStorer.Create(overrideFpkgPath, ""))
            {
                using (ZipStorer zip = Open(zipFilePath, FileAccess.Read))
                {
                    if (!Directory.Exists(extraDir))
                    {
                        Directory.CreateDirectory(extraDir);
                    }

                    zip.ReadCentralDirAction(delegate(ZipFileEntry f)
                    {
                        string targetFilePath = Path.Combine(extraDir, f.FilenameInZip);
                        bool backupSuccess    = true;
                        if (File.Exists(targetFilePath))
                        {
                            sw.Write(string.Format("#备份文件{0}", f.FilenameInZip));
                            try
                            {
                                zipBak.AddFile(Compression.Deflate, targetFilePath, f.FilenameInZip, "");
                                sw.WriteLine(" OK!");
                                successCount++;
                            }
                            catch (Exception bakEx)
                            {
                                backupSuccess = false;
                                sw.WriteLine();
                                sw.WriteLine(string.Format("\n#{0} 备份失败 \n#{1}", f.FilenameInZip, bakEx.Message.Replace("\n", "\n#")));
                                failedBakCount++;
                            }
                        }

                        if (!backupSuccess)
                        {
                            sw.WriteLine(string.Format("## {0} 因备份失败未更新", f.FilenameInZip));
                        }
                        else
                        {
                            try
                            {
                                zip.ExtractFile(f, targetFilePath);
                                overrideCount++;
                            }
                            catch (Exception ioEx)
                            {
                                sw.WriteLine(string.Format("\n##*{0} 覆盖失败 \n#{1}", f.FilenameInZip, ioEx.Message.Replace("\n", "\n#")));
                            }
                        }
                    });
                }
            }

            sw.WriteLine(string.Format("# -> 共备份成功{0}个文件,{1}个失败,覆盖{2}个文件,{3}个文件未被更新!", successCount, failedBakCount, overrideCount, successCount - overrideCount));

            sw.Flush();
            sw.Close();

            File.WriteAllBytes(overrideFpkgPath.Replace(".zip", ".log"), ms.ToArray());

            ms.Close();
            ms.Dispose();
        }