/* * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals. * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ using System.IO; using System.IO.Compression; using System.Text; namespace QuantConnect { /// /// Provides an implementation of to write to a zip file /// public class ZipStreamWriter : TextWriter { private readonly ZipArchive _archive; private readonly StreamWriter _writer; /// /// When overridden in a derived class, returns the character encoding in which the output is written. /// /// /// The character encoding in which the output is written. /// /// 1 public override Encoding Encoding => Encoding.Default; /// /// Initializes a new instance of the class /// /// The output zip file name /// The file name in the zip file public ZipStreamWriter(string filename, string zipEntry) { if(!File.Exists(filename)) { _archive = ZipFile.Open(filename, ZipArchiveMode.Create); var entry = _archive.CreateEntry(zipEntry); _writer = new StreamWriter(entry.Open()); } else { _archive = ZipFile.Open(filename, ZipArchiveMode.Update); var entry = _archive.GetEntry(zipEntry); var nonExisting = entry == null; if (nonExisting) { entry = _archive.CreateEntry(zipEntry); } _writer = new StreamWriter(entry.Open()); if (!nonExisting) { // can only seek when it already existed _writer.BaseStream.Seek(0L, SeekOrigin.End); } } } /// /// Writes a character to the text string or stream. /// /// The character to write to the text stream. /// The is closed. /// An I/O error occurs. /// 1 public override void Write(char value) { _writer.Write(value); } /// /// Writes a string followed by a line terminator to the text string or stream. /// /// The string to write. If is null, only the line terminator is written. /// The is closed. /// An I/O error occurs. /// 1 public override void WriteLine(string value) { _writer.WriteLine(value); } /// /// Clears all buffers for the current writer and causes any buffered data to be written to the underlying device. /// public override void Flush() { _writer.Flush(); } /// /// Releases the unmanaged resources used by the and optionally releases the managed resources. /// /// true to release both managed and unmanaged resources; false to release only unmanaged resources. protected override void Dispose(bool disposing) { if (_writer == null || !disposing) return; _writer.Flush(); _writer.Close(); _writer.Dispose(); _archive.Dispose(); } } }