You work as an application developer at Domain.com. You are currently in the process of creating a new application for Domain.com. You are required to read compressed data files that has been sent by Domain.com’s sales offices.
These data files are less than 4 GB in size, but was compressed without cyclic redundancy. You want to write a method that receives the compressed files and return the uncompressed data as a byte array.
What should you do?
A.
Use the following code:
public byte [] DecompressFile(string file){
FileStream fs = new FileStream(file, FileMode.Open);
DeflateStream cs = new DeflateStream(fs, CompressionMode.Decompress, true);
byte [ ] data = new byte [fs.Length – 1];
cs.Read(data, 0, data.Length);
cs.Close ();
return data;
}
B.
Use the following code:
public byte [] DecompressFile(string file){
FileStream fs = new FileStream(file, FileMode.Open);
GZipStream cs = new GZipStream(fs, CompressionMode.Decompress);
byte [ ] data = new byte [fs.Length – 1];
cs.Read(data, 0, data.Length);
return data;
}
C.
Use the following code:
public byte [] DecompressFile(string file){
FileStream fs = new FileStream(file, FileMode.Open);
DeflateStream cs = new DeflateStream (fs, CompressionMode.Decompress);
byte [ ] data = new byte [fs.Length – 1];
cs.Read(data, 0, data.Length);
return data;
}
D.
Use the following code:
public byte [] DecompressFile (string file){
FileStream fs = new FileStream (file, FileMode.Open);
GZipStream cs = new GZipStream (fs, CompressionMode.Decompress, true);
byte [ ] data = new byte [fs.Length – 1];
cs.Read (data, 0, data.Length);
cs.Close ();
return data;
}
Explanation:
The DeflateStream uses the LZ77 and Huffman coding algorithms for lossless compression and decompression without cyclic redundancy.
The DeflateStream constructor takes a stream (in this case an input stream), a CompressionMode enumeration value,
and a Boolean value indicating whether to keep the stream open. The CompressionMode enumeration value indicates whether to compress or decompress the specified stream
using the values Compress and Decompress, respectively. The Read method takes the byte array, offset value, and total number of bytes as arguments. In this code, the compressed data is read from the FileStream object associated with the DeflateStream object,
and it is stored in the byte array.
Like all streams, the DeflateStream object has a Close method, which should be called after work is done with the stream.
Finally, the resulting byte array is returned.
Incorrect Answers:
B, D: You should not use the code fragments that specify the GZipStream class because this data format includes a data corruption check during decompression.
C: You should not use the code fragments that instantiates the Stream objects and invoke the Read method with the wrong arguments.
A