N
N
Nikita2017-09-25 10:10:04
.NET
Nikita, 2017-09-25 10:10:04

Why is the file not processed in blocks?

Wrote 2 methods for archiving and encrypting a file.
1st worker:

public static void CompressAndEncrypt(string sourceFile, string encrFile)
    {
      int bufferSize = 5242880;
      using (var readStream = new FileStream(sourceFile, FileMode.Open, FileAccess.ReadWrite))
      {
        using (var writeStream = new FileStream(encrFile, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite))
        {
          DESCryptoServiceProvider cryptic = new DESCryptoServiceProvider();
          cryptic.Key = ASCIIEncoding.ASCII.GetBytes("ABCDEFGH");
          cryptic.IV = ASCIIEncoding.ASCII.GetBytes("ABCDEFGH");
          using (var crypto = new CryptoStream(writeStream, cryptic.CreateEncryptor(), CryptoStreamMode.Write))
          {
            using (var zip = new GZipStream(crypto, CompressionMode.Compress))
            {
              int bytesRead = -1;
              byte[] bytes = new byte[bufferSize];

              while ((bytesRead = readStream.Read(bytes, 0, bufferSize)) > 0)
              {
                zip.Write(bytes, 0, bytesRead);
              }
            }
          }
        }
      }
    }

2nd non-working:
public static void CompressAndEncryptBlock(string sourceFile, string outputFile)
    {
      int bufferSize = 5242880;
      int bytesRead;
      var bytes = new byte[bufferSize];

      using (var readStream = new FileStream(sourceFile, FileMode.Open, FileAccess.ReadWrite))
      {
        using (var writer = new FileStream(outputFile, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite))
        {
          while ((bytesRead = readStream.Read(bytes, 0, bufferSize)) > 0)
          {
            using (var writeStream = new MemoryStream())
            {
              DESCryptoServiceProvider cryptic = new DESCryptoServiceProvider();
              cryptic.Key = Encoding.ASCII.GetBytes("ABCDEFGH");
              cryptic.IV = Encoding.ASCII.GetBytes("ABCDEFGH");
              using (var crypto = new CryptoStream(writeStream, cryptic.CreateEncryptor(), CryptoStreamMode.Write))
              {
                using (var zip = new GZipStream(crypto, CompressionMode.Compress, true))
                {
                  zip.Write(bytes, 0, bytesRead);
                  //После этого Capacity у writeStream(MemoryStream) почему-то больше чем его Length
                }
                var bytes1 = new byte[writeStream.Length];
                writeStream.Read(bytes1, 0, bytes1.Length);
                writer.Write(bytes1, 0, bytes1.Length);
              }
            }
          }
        }
      }
    }

Why is the file processing incorrect in the second way?
I will need the second method in the future to transfer the file in blocks (for now, I’m just testing it for writing to disk).
If I use the second method, then the output file is slightly smaller than when using the first method.
And when I try to decrypt and unzip the file obtained by the second method, I get an exception
System.IO.InvalidDataException occurred
HResult=0x80131501
Message=Incorrect magic number in the GZip header. The transfer must go to a GZip stream.
Source=System
StackTrace:
at System.IO.Compression.GZipDecoder.ReadHeader(InputBuffer input)
at System.IO.Compression.Inflater.Decode()
at System.IO.Compression.Inflater.Inflate(Byte[] bytes, Int32 offset, Int32 length)
at System.IO.Compression.DeflateStream.Read(Byte[] array, Int32 offset, Int32 count)
at System.IO.Compression .GZipStream.Read(Byte[] array, Int32 offset, Int32 count)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
lam0x86, 2017-09-25
@lam0x86

Perhaps instead of

var bytes1 = new byte[writeStream.Length];
writeStream.Read(bytes1, 0, bytes1.Length);
writer.Write(bytes1, 0, bytes1.Length);

should write something like
writeStream.Position = 0;
writeStream.CopyTo(writer);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question