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

Finish() public méthode

Finishes the stream. This will write the central directory at the end of the zip file and flush the stream.
This is automatically called when the stream is closed.
/// An I/O error occurs. /// /// Comment exceeds the maximum length
/// Entry name exceeds the maximum length ///
public Finish ( ) : void
Résultat void
        public override void Finish()
        {
            if (entries == null) {
                return;
            }

            if (curEntry != null) {
                CloseEntry();
            }

            long numEntries = entries.Count;
            long sizeEntries = 0;

            foreach (ZipEntry entry in entries) {
                WriteLeInt(ZipConstants.CentralHeaderSignature);
                WriteLeShort(ZipConstants.VersionMadeBy);
                WriteLeShort(entry.Version);
                WriteLeShort(entry.Flags);
                WriteLeShort((short)entry.CompressionMethodForHeader);
                WriteLeInt((int)entry.DosTime);
                WriteLeInt((int)entry.Crc);

                if (entry.IsZip64Forced() ||
                    (entry.CompressedSize >= uint.MaxValue)) {
                    WriteLeInt(-1);
                } else {
                    WriteLeInt((int)entry.CompressedSize);
                }

                if (entry.IsZip64Forced() ||
                    (entry.Size >= uint.MaxValue)) {
                    WriteLeInt(-1);
                } else {
                    WriteLeInt((int)entry.Size);
                }

                byte[] name = ZipConstants.ConvertToArray(entry.Flags, entry.Name);

                if (name.Length > 0xffff) {
                    throw new ZipException("Name too long.");
                }

                var ed = new ZipExtraData(entry.ExtraData);

                if (entry.CentralHeaderRequiresZip64) {
                    ed.StartNewEntry();
                    if (entry.IsZip64Forced() ||
                        (entry.Size >= 0xffffffff)) {
                        ed.AddLeLong(entry.Size);
                    }

                    if (entry.IsZip64Forced() ||
                        (entry.CompressedSize >= 0xffffffff)) {
                        ed.AddLeLong(entry.CompressedSize);
                    }

                    if (entry.Offset >= 0xffffffff) {
                        ed.AddLeLong(entry.Offset);
                    }

                    ed.AddNewEntry(1);
                } else {
                    ed.Delete(1);
                }

                if (entry.AESKeySize > 0) {
                    AddExtraDataAES(entry, ed);
                }
                byte[] extra = ed.GetEntryData();

                byte[] entryComment =
                    (entry.Comment != null) ?
                    ZipConstants.ConvertToArray(entry.Flags, entry.Comment) :
                    new byte[0];

                if (entryComment.Length > 0xffff) {
                    throw new ZipException("Comment too long.");
                }

                WriteLeShort(name.Length);
                WriteLeShort(extra.Length);
                WriteLeShort(entryComment.Length);
                WriteLeShort(0);    // disk number
                WriteLeShort(0);    // internal file attributes
                                    // external file attributes

                if (entry.ExternalFileAttributes != -1) {
                    WriteLeInt(entry.ExternalFileAttributes);
                } else {
                    if (entry.IsDirectory) {                         // mark entry as directory (from nikolam.AT.perfectinfo.com)
                        WriteLeInt(16);
                    } else {
                        WriteLeInt(0);
                    }
                }

                if (entry.Offset >= uint.MaxValue) {
                    WriteLeInt(-1);
                } else {
                    WriteLeInt((int)entry.Offset);
                }

                if (name.Length > 0) {
                    baseOutputStream_.Write(name, 0, name.Length);
                }

                if (extra.Length > 0) {
                    baseOutputStream_.Write(extra, 0, extra.Length);
                }

                if (entryComment.Length > 0) {
                    baseOutputStream_.Write(entryComment, 0, entryComment.Length);
                }

                sizeEntries += ZipConstants.CentralHeaderBaseSize + name.Length + extra.Length + entryComment.Length;
            }

            using (ZipHelperStream zhs = new ZipHelperStream(baseOutputStream_)) {
                zhs.WriteEndOfCentralDirectory(numEntries, sizeEntries, offset, zipComment);
            }

            entries = null;
        }

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::Finish