3
3
using ICSharpCode . SharpZipLib . Zip . Compression . Streams ;
4
4
using System ;
5
5
using System . IO ;
6
+ using System . Text ;
6
7
7
8
namespace ICSharpCode . SharpZipLib . GZip
8
9
{
@@ -54,6 +55,8 @@ public class GZipInputStream : InflaterInputStream
54
55
/// </summary>
55
56
private bool completedLastBlock ;
56
57
58
+ private string fileName ;
59
+
57
60
#endregion Instance Fields
58
61
59
62
#region Constructors
@@ -149,6 +152,15 @@ public override int Read(byte[] buffer, int offset, int count)
149
152
}
150
153
}
151
154
155
+ /// <summary>
156
+ /// Retrieves the filename header field for the block last read
157
+ /// </summary>
158
+ /// <returns></returns>
159
+ public string GetFilename ( )
160
+ {
161
+ return fileName ;
162
+ }
163
+
152
164
#endregion Stream overrides
153
165
154
166
#region Support routines
@@ -170,149 +182,108 @@ private bool ReadHeader()
170
182
}
171
183
}
172
184
173
- // 1. Check the two magic bytes
174
185
var headCRC = new Crc32 ( ) ;
175
- int magic = inputBuffer . ReadLeByte ( ) ;
176
186
177
- if ( magic < 0 )
178
- {
179
- throw new EndOfStreamException ( "EOS reading GZIP header" ) ;
180
- }
187
+ // 1. Check the two magic bytes
181
188
189
+ var magic = inputBuffer . ReadLeByte ( ) ;
182
190
headCRC . Update ( magic ) ;
183
- if ( magic != ( GZipConstants . GZIP_MAGIC >> 8 ) )
191
+ if ( magic != GZipConstants . ID1 )
184
192
{
185
193
throw new GZipException ( "Error GZIP header, first magic byte doesn't match" ) ;
186
194
}
187
195
188
- //magic = baseInputStream.ReadByte();
189
196
magic = inputBuffer . ReadLeByte ( ) ;
190
-
191
- if ( magic < 0 )
192
- {
193
- throw new EndOfStreamException ( "EOS reading GZIP header" ) ;
194
- }
195
-
196
- if ( magic != ( GZipConstants . GZIP_MAGIC & 0xFF ) )
197
+ if ( magic != GZipConstants . ID2 )
197
198
{
198
199
throw new GZipException ( "Error GZIP header, second magic byte doesn't match" ) ;
199
200
}
200
-
201
201
headCRC . Update ( magic ) ;
202
202
203
203
// 2. Check the compression type (must be 8)
204
- int compressionType = inputBuffer . ReadLeByte ( ) ;
205
-
206
- if ( compressionType < 0 )
207
- {
208
- throw new EndOfStreamException ( "EOS reading GZIP header" ) ;
209
- }
204
+ var compressionType = inputBuffer . ReadLeByte ( ) ;
210
205
211
- if ( compressionType != 8 )
206
+ if ( compressionType != GZipConstants . CompressionMethodDeflate )
212
207
{
213
208
throw new GZipException ( "Error GZIP header, data not in deflate format" ) ;
214
209
}
215
210
headCRC . Update ( compressionType ) ;
216
211
217
212
// 3. Check the flags
218
- int flags = inputBuffer . ReadLeByte ( ) ;
219
- if ( flags < 0 )
220
- {
221
- throw new EndOfStreamException ( "EOS reading GZIP header" ) ;
222
- }
223
- headCRC . Update ( flags ) ;
224
-
225
- /* This flag byte is divided into individual bits as follows:
213
+ var flagsByte = inputBuffer . ReadLeByte ( ) ;
226
214
227
- bit 0 FTEXT
228
- bit 1 FHCRC
229
- bit 2 FEXTRA
230
- bit 3 FNAME
231
- bit 4 FCOMMENT
232
- bit 5 reserved
233
- bit 6 reserved
234
- bit 7 reserved
235
- */
215
+ headCRC . Update ( flagsByte ) ;
236
216
237
217
// 3.1 Check the reserved bits are zero
238
218
239
- if ( ( flags & 0xE0 ) != 0 )
219
+ if ( ( flagsByte & 0xE0 ) != 0 )
240
220
{
241
221
throw new GZipException ( "Reserved flag bits in GZIP header != 0" ) ;
242
222
}
243
223
224
+ var flags = ( GZipFlags ) flagsByte ;
225
+
244
226
// 4.-6. Skip the modification time, extra flags, and OS type
245
227
for ( int i = 0 ; i < 6 ; i ++ )
246
228
{
247
- int readByte = inputBuffer . ReadLeByte ( ) ;
248
- if ( readByte < 0 )
249
- {
250
- throw new EndOfStreamException ( "EOS reading GZIP header" ) ;
251
- }
252
- headCRC . Update ( readByte ) ;
229
+ headCRC . Update ( inputBuffer . ReadLeByte ( ) ) ;
253
230
}
254
231
255
232
// 7. Read extra field
256
- if ( ( flags & GZipConstants . FEXTRA ) != 0 )
233
+ if ( flags . HasFlag ( GZipFlags . FEXTRA ) )
257
234
{
258
235
// XLEN is total length of extra subfields, we will skip them all
259
- int len1 , len2 ;
260
- len1 = inputBuffer . ReadLeByte ( ) ;
261
- len2 = inputBuffer . ReadLeByte ( ) ;
262
- if ( ( len1 < 0 ) || ( len2 < 0 ) )
263
- {
264
- throw new EndOfStreamException ( "EOS reading GZIP header" ) ;
265
- }
236
+ var len1 = inputBuffer . ReadLeByte ( ) ;
237
+ var len2 = inputBuffer . ReadLeByte ( ) ;
238
+
266
239
headCRC . Update ( len1 ) ;
267
240
headCRC . Update ( len2 ) ;
268
241
269
242
int extraLen = ( len2 << 8 ) | len1 ; // gzip is LSB first
270
243
for ( int i = 0 ; i < extraLen ; i ++ )
271
244
{
272
- int readByte = inputBuffer . ReadLeByte ( ) ;
273
- if ( readByte < 0 )
274
- {
275
- throw new EndOfStreamException ( "EOS reading GZIP header" ) ;
276
- }
277
- headCRC . Update ( readByte ) ;
245
+ headCRC . Update ( inputBuffer . ReadLeByte ( ) ) ;
278
246
}
279
247
}
280
248
281
249
// 8. Read file name
282
- if ( ( flags & GZipConstants . FNAME ) != 0 )
250
+ if ( flags . HasFlag ( GZipFlags . FNAME ) )
283
251
{
252
+ var fname = new byte [ 1024 ] ;
253
+ var fnamePos = 0 ;
284
254
int readByte ;
285
255
while ( ( readByte = inputBuffer . ReadLeByte ( ) ) > 0 )
286
256
{
257
+ if ( fnamePos < 1024 )
258
+ {
259
+ fname [ fnamePos ++ ] = ( byte ) readByte ;
260
+ }
287
261
headCRC . Update ( readByte ) ;
288
262
}
289
263
290
- if ( readByte < 0 )
291
- {
292
- throw new EndOfStreamException ( "EOS reading GZIP header" ) ;
293
- }
294
264
headCRC . Update ( readByte ) ;
265
+
266
+ fileName = GZipConstants . Encoding . GetString ( fname , 0 , fnamePos ) ;
267
+ }
268
+ else
269
+ {
270
+ fileName = null ;
295
271
}
296
272
297
273
// 9. Read comment
298
- if ( ( flags & GZipConstants . FCOMMENT ) != 0 )
274
+ if ( flags . HasFlag ( GZipFlags . FCOMMENT ) )
299
275
{
300
276
int readByte ;
301
277
while ( ( readByte = inputBuffer . ReadLeByte ( ) ) > 0 )
302
278
{
303
279
headCRC . Update ( readByte ) ;
304
280
}
305
281
306
- if ( readByte < 0 )
307
- {
308
- throw new EndOfStreamException ( "EOS reading GZIP header" ) ;
309
- }
310
-
311
282
headCRC . Update ( readByte ) ;
312
283
}
313
284
314
285
// 10. Read header CRC
315
- if ( ( flags & GZipConstants . FHCRC ) != 0 )
286
+ if ( flags . HasFlag ( GZipFlags . FHCRC ) )
316
287
{
317
288
int tempByte ;
318
289
int crcval = inputBuffer . ReadLeByte ( ) ;
0 commit comments