ICSharpCode.SharpZipLib.Zip.ZipOutputStream.Write C# (CSharp) Méthode

Write() public méthode

Writes the given buffer to the current entry.
Archive size is invalid No entry is active.
public Write ( byte buffer, int offset, int count ) : void
buffer byte The buffer containing data to write.
offset int The offset of the first byte to write.
count int The number of bytes to write.
Résultat void
        public override void Write(byte[] buffer, int offset, int count)
        {
            if (curEntry == null) {
                throw new InvalidOperationException("No open entry.");
            }

            if (buffer == null) {
                throw new ArgumentNullException(nameof(buffer));
            }

            if (offset < 0) {
                throw new ArgumentOutOfRangeException(nameof(offset), "Cannot be negative");
            }

            if (count < 0) {
                throw new ArgumentOutOfRangeException(nameof(count), "Cannot be negative");
            }

            if ((buffer.Length - offset) < count) {
                throw new ArgumentException("Invalid offset/count combination");
            }

            crc.Update(buffer, offset, count);
            size += count;

            switch (curMethod) {
                case CompressionMethod.Deflated:
                    base.Write(buffer, offset, count);
                    break;

                case CompressionMethod.Stored:
                    if (Password != null) {
                        CopyAndEncrypt(buffer, offset, count);
                    } else {
                        baseOutputStream_.Write(buffer, offset, count);
                    }
                    break;
            }
        }

Usage Example

Exemple #1
1
        /// <summary>
        /// This function creates a zip
        /// </summary>
        /// <param name="filepaths">List of absolute system filepaths</param>
        /// <param name="zipFileName">Absolute desired systeme final zip filepath</param>
        /// <param name="compressionLevel">Compression level from 0 (no comp.) to 9 (best comp.)</param>
        /// <returns></returns>
        public StdResult<NoType> CreateZip(List<string> filepaths, string zipFileName, int compressionLevel)
        {
            try
            {
                using (ZipOutputStream s = new ZipOutputStream(File.Create(zipFileName)))
                {
                    s.SetLevel(9); // 0 - store only to 9 - means best compression

                    byte[] buffer = new byte[4096];

                    foreach (string file in filepaths)
                    {

                        // Using GetFileName makes the result compatible with XP
                        // as the resulting path is not absolute.
                        ZipEntry entry = new ZipEntry(Path.GetFileName(file));

                        // Setup the entry data as required.

                        // Crc and size are handled by the library for seakable streams
                        // so no need to do them here.

                        // Could also use the last write time or similar for the file.
                        entry.DateTime = DateTime.Now;
                        s.PutNextEntry(entry);

                        using (FileStream fs = File.OpenRead(file))
                        {

                            // Using a fixed size buffer here makes no noticeable difference for output
                            // but keeps a lid on memory usage.
                            int sourceBytes;
                            do
                            {
                                sourceBytes = fs.Read(buffer, 0, buffer.Length);
                                s.Write(buffer, 0, sourceBytes);
                            } while (sourceBytes > 0);
                        }
                    }

                    // Finish/Close arent needed strictly as the using statement does this automatically

                    // Finish is important to ensure trailing information for a Zip file is appended.  Without this
                    // the created file would be invalid.
                    s.Finish();

                    // Close is important to wrap things up and unlock the file.
                    s.Close();
                    return StdResult<NoType>.OkResult;
                }
            }
            catch (Exception ex)
            {
                return StdResult<NoType>.BadResult(ex.Message);

                // No need to rethrow the exception as for our purposes its handled.
            }
        }
All Usage Examples Of ICSharpCode.SharpZipLib.Zip.ZipOutputStream::Write