@@ -70,14 +70,18 @@ CompressionType compressionType
70
70
_rollOnFileSizeLimit = rollOnFileSizeLimit ;
71
71
}
72
72
73
+ /// <summary>
74
+ /// Chooses which compression algorithm to run on the log file passed in within the given directory.
75
+ /// </summary>
76
+ /// <param name="logFile"> The log file to be compressed </param>
77
+ /// <param name="logDirectory"> The directory that holds logFile </param>
78
+ /// <param name="compressionType"> The compression algorithm to be used. Is of type CompressionType </param>
73
79
public void Compress ( string logFile , string logDirectory , CompressionType compressionType )
74
- {
75
- var readDirectory = Path . Combine ( logDirectory , "new_dir" ) ;
76
-
80
+ {
77
81
switch ( compressionType )
78
82
{
79
83
case CompressionType . Zip :
80
- ZipCompress ( logFile , logDirectory , readDirectory ) ;
84
+ ZipCompress ( logFile , logDirectory ) ;
81
85
break ;
82
86
case CompressionType . GZip :
83
87
GZipCompress ( logFile , logDirectory ) ;
@@ -87,39 +91,51 @@ public void Compress(string logFile, string logDirectory, CompressionType compre
87
91
}
88
92
}
89
93
90
- public void ZipCompress ( string prevLog , string logDirectory , string readDirectory )
94
+ /// <summary>
95
+ /// Uses Zip compression to compress prevLog into a new, zipped directory.
96
+ /// The Zip algorithm is from System.IO.Compression
97
+ /// </summary>
98
+ /// <param name="logFile"> The log file to be compressed </param>
99
+ /// <param name="logDirectory"> The directory that holds logFile </param>
100
+ public void ZipCompress ( string logFile , string logDirectory )
91
101
{
102
+ var readDirectory = Path . Combine ( logDirectory , "new_dir" ) ;
92
103
Directory . CreateDirectory ( readDirectory ) ;
93
104
System . IO . File . Move (
94
- Path . Combine ( logDirectory , prevLog ) , Path . Combine ( readDirectory , prevLog )
105
+ Path . Combine ( logDirectory , logFile ) , Path . Combine ( readDirectory , logFile )
95
106
) ;
96
107
97
- var zipName = prevLog . Remove ( prevLog . Length - 4 ) ;
98
- var zip_path = Path . Combine ( logDirectory , $ "{ zipName } -Zip .zip") ;
108
+ var zipName = Path . GetFileNameWithoutExtension ( logFile ) ;
109
+ var zip_path = Path . Combine ( logDirectory , $ "{ zipName } .zip") ;
99
110
System . IO . Compression . ZipFile . CreateFromDirectory ( readDirectory , zip_path ) ;
100
111
101
112
Directory . Delete ( readDirectory , true ) ;
102
113
}
103
114
104
- public void GZipCompress ( string prevLog , string logDirectory )
115
+ /// <summary>
116
+ /// Uses GZip compression algorithm to compress logFile into a .gzip file
117
+ /// The GZip algorithm is from System.IO.Compression
118
+ /// </summary>
119
+ /// <param name="logFile"> The log file to be compressed </param>
120
+ /// <param name="logDirectory"> The directory that holds logFile </param>
121
+ public void GZipCompress ( string logFile , string logDirectory )
105
122
{
106
- var logPath = Path . Combine ( logDirectory , prevLog ) ;
107
- byte [ ] byteArray ;
108
- using ( FileStream prevLogStream = new FileStream ( logPath , FileMode . Open ) )
109
- {
110
- byteArray = new byte [ prevLogStream . Length ] ;
111
- prevLogStream . Read ( byteArray , 0 , ( int ) prevLogStream . Length ) ;
112
- }
123
+ var logPath = Path . Combine ( logDirectory , logFile ) ;
124
+ byte [ ] byteArray = new byte [ ] { } ;
125
+
126
+ byteArray = System . IO . File . ReadAllBytes ( logPath ) ;
113
127
114
- var logName = prevLog . Remove ( prevLog . Length - 4 ) ;
115
- var GZipPath = Path . Combine ( logDirectory , $ "{ logName } -GZip .gz") ;
128
+ var logName = Path . GetFileNameWithoutExtension ( logFile ) ;
129
+ var GZipPath = Path . Combine ( logDirectory , $ "{ logName } .gz") ;
116
130
117
131
using ( FileStream outFile = new FileStream ( GZipPath , FileMode . Create ) )
118
132
using ( System . IO . Compression . GZipStream gzipStream = new System . IO . Compression . GZipStream ( outFile , System . IO . Compression . CompressionMode . Compress , false ) )
119
133
{
120
134
gzipStream . Write ( byteArray , 0 , byteArray . Length ) ;
121
135
}
122
136
137
+ System . IO . File . WriteAllBytes ( GZipPath , byteArray ) ;
138
+
123
139
System . IO . File . Delete ( logPath ) ;
124
140
}
125
141
@@ -174,7 +190,8 @@ void AlignCurrentFileTo(DateTime now, bool nextSequence = false)
174
190
{
175
191
var directoryFileName = Path . GetFileName ( directoryFile ) ;
176
192
177
- if ( ! ( directoryFileName . Contains ( "-GZip" ) ) )
193
+ // Not sure if we can assume log files will always be .txt?
194
+ if ( System . IO . Path . GetExtension ( directoryFileName ) == ".txt" )
178
195
{
179
196
Compress ( directoryFileName , logDirectory , _compressionType ) ;
180
197
}
0 commit comments