System.IO.Compression.ZipStorer.AddStream C# (CSharp) Méthode

AddStream() public méthode

Add full contents of a stream into the Zip storage
public AddStream ( Compression _method, string _filenameInZip, System.Stream _source, System.DateTime _modTime, string _comment, ushort access = 0x8100 ) : void
_method Compression Compression method
_filenameInZip string Filename and path as desired in Zip directory
_source System.Stream Stream object containing the data to store in Zip
_modTime System.DateTime Modification time of the data to store
_comment string Comment for stored file
access ushort
Résultat void
        public void AddStream(Compression _method, string _filenameInZip, Stream _source, DateTime _modTime, string _comment, ushort access = 0x8100)
        {
            if (Access == FileAccess.Read)
                throw new InvalidOperationException("Writing is not alowed");

            //long offset;
            if (this.Files.Count==0)
                ;//offset = 0;
            else
            {
                /*ZipFileEntry last = this.*/Files[this.Files.Count-1];
                // /*offset =*/ last.HeaderOffset + last.HeaderSize;
            }

            // Prepare the fileinfo
            ZipFileEntry zfe = new ZipFileEntry();
            if (_source.Length < 10)
                zfe.Method = Compression.Store;
            else
                zfe.Method = _method;
            zfe.EncodeUTF8 = this.EncodeUTF8;
            zfe.FilenameInZip = NormalizedFilename(_filenameInZip);
            zfe.Comment = (_comment == null ? "" : _comment);
            zfe.ExternalFileAttributes = 0x8000 | access & 0xfff; // file

            // Even though we write the header now, it will have to be rewritten, since we don't know compressed size or crc.
            zfe.Crc32 = 0;  // to be updated later
            zfe.HeaderOffset = (uint)this.ZipFileStream.Position;  // offset within file of the start of this local record
            zfe.ModifyTime = _modTime;

            // Write local header
            WriteLocalHeader(ref zfe);
            zfe.FileOffset = (uint)this.ZipFileStream.Position;

            // Write file to zip (store)
            Store(ref zfe, _source);
            _source.Close();

            this.UpdateCrcAndSizes(ref zfe);

            Files.Add(zfe);
        }

Usage Example

        //Epubにファイルを追加する(mimetypeを除く)
        private static void WriteEpubFilesToZip(ZipStorer zip,string srcDir)
        {
            var files = Directory.GetFiles(srcDir, "*", SearchOption.AllDirectories);           //全ファイル
            var targetFiles = files.Where(e => Path.GetFileName(e).Equals("mimetype") != true)  //mimetypeを除く
                .Select(e => new FileInfo(e));

            foreach (var targetFile in targetFiles)
            {
                var ext = targetFile.Extension;
                var compression = new ZipStorer.Compression();
                switch (ext)
                {
                    case "jpg": //画像ファイルは圧縮しない(時間の無駄なので)
                    case "JPEG":
                    case "png":
                    case "PNG":
                    case "gif":
                    case "GIF":
                        compression = ZipStorer.Compression.Store;
                        break;
                    case "EPUB":
                    case "epub":
                        continue;   //EPUBファイルは格納しない
                    default:
                        compression = ZipStorer.Compression.Deflate;  //通常のファイルは圧縮する
                        break;
                }
                //対象を書き込む
                using (var ms = new MemoryStream(File.ReadAllBytes(targetFile.FullName)))
                {
                    ms.Position = 0;    //ファイルの先頭からコピー
                    var fileNameInZip = GetRelPath(targetFile.FullName, srcDir);    //zip内でのファイル名
                    zip.AddStream(compression, fileNameInZip, ms, DateTime.Now, string.Empty);
                }
            }
        }
All Usage Examples Of System.IO.Compression.ZipStorer::AddStream