Lean  $LEAN_TAG$
ZipStreamWriter.cs
1 /*
2  * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
3  * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14 */
15 
16 using System.IO;
17 using System.IO.Compression;
18 using System.Text;
19 
20 namespace QuantConnect
21 {
22  /// <summary>
23  /// Provides an implementation of <see cref="TextWriter"/> to write to a zip file
24  /// </summary>
25  public class ZipStreamWriter : TextWriter
26  {
27  private readonly ZipArchive _archive;
28  private readonly StreamWriter _writer;
29 
30  /// <summary>
31  /// When overridden in a derived class, returns the character encoding in which the output is written.
32  /// </summary>
33  /// <returns>
34  /// The character encoding in which the output is written.
35  /// </returns>
36  /// <filterpriority>1</filterpriority>
37  public override Encoding Encoding => Encoding.Default;
38 
39  /// <summary>
40  /// Initializes a new instance of the <see cref="ZipStreamWriter"/> class
41  /// </summary>
42  /// <param name="filename">The output zip file name</param>
43  /// <param name="zipEntry">The file name in the zip file</param>
44  public ZipStreamWriter(string filename, string zipEntry)
45  {
46  if(!File.Exists(filename))
47  {
48  _archive = ZipFile.Open(filename, ZipArchiveMode.Create);
49  var entry = _archive.CreateEntry(zipEntry);
50  _writer = new StreamWriter(entry.Open());
51  }
52  else
53  {
54  _archive = ZipFile.Open(filename, ZipArchiveMode.Update);
55  var entry = _archive.GetEntry(zipEntry);
56  var nonExisting = entry == null;
57  if (nonExisting)
58  {
59  entry = _archive.CreateEntry(zipEntry);
60  }
61  _writer = new StreamWriter(entry.Open());
62 
63  if (!nonExisting)
64  {
65  // can only seek when it already existed
66  _writer.BaseStream.Seek(0L, SeekOrigin.End);
67  }
68  }
69  }
70 
71  /// <summary>
72  /// Writes a character to the text string or stream.
73  /// </summary>
74  /// <param name="value">The character to write to the text stream. </param>
75  /// <exception cref="T:System.ObjectDisposedException">The <see cref="T:System.IO.TextWriter"/> is closed. </exception>
76  /// <exception cref="T:System.IO.IOException">An I/O error occurs. </exception>
77  /// <filterpriority>1</filterpriority>
78  public override void Write(char value)
79  {
80  _writer.Write(value);
81  }
82 
83  /// <summary>
84  /// Writes a string followed by a line terminator to the text string or stream.
85  /// </summary>
86  /// <param name="value">The string to write. If <paramref name="value"/> is null, only the line terminator is written. </param>
87  /// <exception cref="T:System.ObjectDisposedException">The <see cref="T:System.IO.TextWriter"/> is closed. </exception>
88  /// <exception cref="T:System.IO.IOException">An I/O error occurs. </exception>
89  /// <filterpriority>1</filterpriority>
90  public override void WriteLine(string value)
91  {
92  _writer.WriteLine(value);
93  }
94 
95  /// <summary>
96  /// Clears all buffers for the current writer and causes any buffered data to be written to the underlying device.
97  /// </summary>
98  public override void Flush()
99  {
100  _writer.Flush();
101  }
102 
103  /// <summary>
104  /// Releases the unmanaged resources used by the <see cref="T:System.IO.TextWriter"/> and optionally releases the managed resources.
105  /// </summary>
106  /// <param name="disposing">true to release both managed and unmanaged resources; false to release only unmanaged resources. </param>
107  protected override void Dispose(bool disposing)
108  {
109  if (_writer == null || !disposing)
110  return;
111  _writer.Flush();
112  _writer.Close();
113  _writer.Dispose();
114  _archive.Dispose();
115  }
116  }
117 }