Ejemplo n.º 1
0
        /// <summary>
        /// Method to open an existing storage from stream
        /// </summary>
        /// <param name="_stream">Already opened stream with zip contents</param>
        /// <param name="_access">File access mode for stream operations</param>
        /// <returns>A valid ZipStorer object</returns>
        public static ZipStorer Open(Stream _stream, FileAccess _access)
        {
            if (!_stream.CanSeek && _access != FileAccess.Read)
            {
                throw new InvalidOperationException("Stream cannot seek");
            }

            ZipStorer zip = new ZipStorer();

            //zip.FileName = _filename;
            zip.ZipFileStream = _stream;
            zip.Access        = _access;

            if (zip.ReadFileInfo())
            {
                zip.ReadCentralDirAction(zip.Files.Add);
                return(zip);
            }

            throw new System.IO.InvalidDataException();
        }
Ejemplo n.º 2
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();
        }