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