Dispose() protected method

Dispose the stream

This method writes the Zip Central directory, then closes the stream. The application must call Dispose() (or Close) in order to produce a valid zip file.

Typically the application will call Dispose() implicitly, via a using statement in C#, or a Using statement in VB.

protected Dispose ( bool disposing ) : void
disposing bool set this to true, always.
return void
Example #1
0
            /// Raises exception on failure.
            /// On failure, existing file is untouched.
            public void Commit()
            {
                if (m_finished)
                {
                    return;
                }
                m_finished = true;

                if (m_zipstream != null)
                {
                    m_zipstream.Dispose();
                    m_zipstream = null;
                }

                string previous = m_destination + "_previous";

                Destroy(previous);
                // Don't destroy previous version until we know the new version is in place.
                try { Rename(m_destination, previous); }
                // The *NotFound exceptions are benign; they happen when writing a new file.
                // Let the other IOExceptions bubble up; they probably indicate some problem
                catch (FileNotFoundException) {}
                catch (DirectoryNotFoundException) {}
                Rename(m_temporaryPath, m_destination);
                Destroy(previous);
            }
        public void WriteCompressedZip(string filepath)
        {
            ZipOutputStream outstream = new ZipOutputStream(filepath);
            outstream.CompressionLevel = Ionic.Zlib.CompressionLevel.BestSpeed;
            BinaryFormatter formatter = new BinaryFormatter();
            outstream.PutNextEntry("dump");


            //Logger.WriteLineTimed("Started compressing search dump");
            DateTime startTime = DateTime.Now;

            formatter.Serialize(outstream, searchDump.StartAddress);
            formatter.Serialize(outstream, searchDump.EndAddress);
            outstream.Write(searchDump.mem, 0, (int)(searchDump.EndAddress - searchDump.StartAddress));

            DateTime endTime = DateTime.Now;
            //Logger.WriteLineTimed("Finished compressing search dump in " + (new TimeSpan(endTime.Ticks - startTime.Ticks).TotalSeconds));


            outstream.PutNextEntry("list");

            //Logger.WriteLineTimed("Started copying search list");
            startTime = DateTime.Now;

            List<UInt32> copy = new List<uint>(resultsList);

            endTime = DateTime.Now;
            //Logger.WriteLineTimed("Finished copying search list in " + (new TimeSpan(endTime.Ticks - startTime.Ticks).TotalSeconds));


            //Logger.WriteLineTimed("Started compressing search list");
            startTime = DateTime.Now;

            formatter.Serialize(outstream, resultsList);

            endTime = DateTime.Now;
            //Logger.WriteLineTimed("Finished compressing search list in " + (new TimeSpan(endTime.Ticks - startTime.Ticks).TotalSeconds));


            outstream.Close();
            outstream.Dispose();
        }
        public void SaveSearch(string filepath, List<UInt32> resultsList, Dump searchDump)
        {
            ZipOutputStream outstream = new ZipOutputStream(filepath);
            outstream.CompressionLevel = Ionic.Zlib.CompressionLevel.BestSpeed;
            BinaryFormatter formatter = new BinaryFormatter();

            // First entry is the dump
            outstream.PutNextEntry("dump");

            //DateTime start = Logger.WriteLineTimedStarted("compressing search dump");

            // Must put the addresses first, so that it can derive the right number of bytes to read for the dump
            formatter.Serialize(outstream, searchDump.StartAddress);
            formatter.Serialize(outstream, searchDump.EndAddress);
            outstream.Write(searchDump.mem, 0, (int)(searchDump.EndAddress - searchDump.StartAddress));

            //Logger.WriteLineTimedFinished("compressing search dump", start);

            // Second entry is the list
            outstream.PutNextEntry("list");

            //start = Logger.WriteLineTimedStarted("compressing search list");

            formatter.Serialize(outstream, resultsList);

            //Logger.WriteLineTimedFinished("compressing search list", start);

            outstream.Close();
            outstream.Dispose();
        }
        private Stream CompressToZip(ItemNameValueCollection entriesPathId)
        {
            var stream = TempStream.Create();
            using (var zip = new ZipOutputStream(stream, true))
            {
                zip.CompressionLevel = CompressionLevel.Level3;
                zip.AlternateEncodingUsage = ZipOption.AsNecessary;
                zip.AlternateEncoding = Encoding.GetEncoding(Thread.CurrentThread.CurrentCulture.TextInfo.OEMCodePage);

                foreach (var path in entriesPathId.AllKeys)
                {
                    if (Canceled)
                    {
                        zip.Dispose();
                        stream.Dispose();
                        return null;
                    }

                    var counter = 0;
                    foreach (var entryId in entriesPathId[path])
                    {
                        var newtitle = path;

                        File file = null;
                        var convertToExt = string.Empty;

                        if (!string.IsNullOrEmpty(entryId))
                        {
                            file = FileDao.GetFile(entryId);

                            if (file.ContentLength > SetupInfo.AvailableFileSize)
                            {
                                Error = string.Format(FilesCommonResource.ErrorMassage_FileSizeZip, FileSizeComment.FilesSizeToString(SetupInfo.AvailableFileSize));
                                continue;
                            }

                            if (_files.ContainsKey(file.ID.ToString()))
                            {
                                if (_quotaDocsEdition || FileUtility.InternalExtension.Values.Contains(convertToExt))
                                    convertToExt = _files[file.ID.ToString()];

                                if (!string.IsNullOrEmpty(convertToExt))
                                {
                                    newtitle = FileUtility.ReplaceFileExtension(path, convertToExt);
                                }
                            }
                        }

                        if (0 < counter)
                        {
                            var suffix = " (" + counter + ")";

                            if (!string.IsNullOrEmpty(entryId))
                            {
                                newtitle = 0 < newtitle.IndexOf('.')
                                               ? newtitle.Insert(newtitle.LastIndexOf('.'), suffix)
                                               : newtitle + suffix;
                            }
                            else
                            {
                                break;
                            }
                        }

                        zip.PutNextEntry(newtitle);

                        if (!string.IsNullOrEmpty(entryId) && file != null)
                        {
                            if (file.ConvertedType != null || !string.IsNullOrEmpty(convertToExt))
                            {
                                //Take from converter
                                try
                                {
                                    using (var readStream = !string.IsNullOrEmpty(convertToExt)
                                                                ? FileConverter.Exec(file, convertToExt)
                                                                : FileConverter.Exec(file))
                                    {
                                        if (readStream != null)
                                        {
                                            readStream.StreamCopyTo(zip);
                                        }
                                    }
                                }
                                catch (Exception ex)
                                {
                                    Error = ex.Message;

                                    Logger.Error(Error, ex);
                                }
                            }
                            else
                            {
                                using (var readStream = FileDao.GetFileStream(file))
                                {
                                    readStream.StreamCopyTo(zip);
                                }
                            }
                        }
                        counter++;
                    }

                    ProgressStep();
                }
                return stream;
            }
        }