locked
Reading and writing files in chunks?

    Question

  • Hi all, been looking all over these forums but can't seem to find any answers that apply exactly to what I need to do. I'm encrypting/decrypting very large files with my app and I need a way of doing this without reading the entire file into memory all at once.

    Something along the lines of this (obviously fake) pseudo-code:

    StorageFile unencryptedFile = await ApplicationData.Current.LocalFolder.GetFileAsync("test.mov");
    StorageFile encryptedFile = await ApplicationData.Current.LocalFolder.CreateFileAsync("test-encrypted.mov");
    
    uint position = 0;
    uint chunkSize = 50000000;
    
    while (true) {
        byte[] unencryptedChunk = unencryptedFile.ReadBytes(position,chunkSize);
    
        if (unencryptedChunk.Length == 0) {
            break;
        }
    
        byte[] encryptedChunk = Encryptor.Encrypt(unencryptedChunk);
    
        encryptedFile.AppendBytes(encryptedChunk);
    
        position += chunkSize;
    }

    Obviously most of those methods don't exists, but hopefully it gets across the point of what I'm trying to do...?


    • Edited by Andrew Ewert Saturday, March 29, 2014 10:43 PM code edit
    Saturday, March 29, 2014 10:42 PM

Answers

  • The StoregeFile is not open yet.  It's just information about the file.  It has methods to open the file, then you can use the it.  eg

                var readStream = unencryptedFile.OpenSequentialReadAsync().GetResults().AsStreamForRead();

    Will open the file and give you a standard .NET Stream over the file contents.

    David


    David http://blogs.msdn.com/b/dbrowne/


    Sunday, March 30, 2014 1:47 AM
  • Yes, you certainly can.

    For example:

     public Stream GetDecryptedStream(Stream encrypted, byte[] Key, byte[] IV)
            {
                SymmetricKeyAlgorithmProvider skap = SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithmNames.TripleDesCbcPkcs7);
    
                CryptographicKey key = skap.CreateSymmetricKey(CryptographicBuffer.CreateFromByteArray(Key));
                IBuffer decrypted = CryptographicEngine.Decrypt(key, encrypted.GetWindowsRuntimeBuffer(), CryptographicBuffer.CreateFromByteArray(IV));
    
                return decrypted.AsStream();
            }

    Sets up a stream that reads an encrypted StorageFile's stream and returns a decrypted form that another library can use.

    If you use the returned stream and do this:

    Stream s = GetDecryptedStream(...);
    
    s.CopyTo(outputStream);
    

    ... it will do it using a small memory buffer, and automatically.

    The same goes with Encryption, just in reverse.


    Darin R.

    Monday, March 31, 2014 4:24 PM

All replies

  • The StoregeFile is not open yet.  It's just information about the file.  It has methods to open the file, then you can use the it.  eg

                var readStream = unencryptedFile.OpenSequentialReadAsync().GetResults().AsStreamForRead();

    Will open the file and give you a standard .NET Stream over the file contents.

    David


    David http://blogs.msdn.com/b/dbrowne/


    Sunday, March 30, 2014 1:47 AM
  • Yes, you certainly can.

    For example:

     public Stream GetDecryptedStream(Stream encrypted, byte[] Key, byte[] IV)
            {
                SymmetricKeyAlgorithmProvider skap = SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithmNames.TripleDesCbcPkcs7);
    
                CryptographicKey key = skap.CreateSymmetricKey(CryptographicBuffer.CreateFromByteArray(Key));
                IBuffer decrypted = CryptographicEngine.Decrypt(key, encrypted.GetWindowsRuntimeBuffer(), CryptographicBuffer.CreateFromByteArray(IV));
    
                return decrypted.AsStream();
            }

    Sets up a stream that reads an encrypted StorageFile's stream and returns a decrypted form that another library can use.

    If you use the returned stream and do this:

    Stream s = GetDecryptedStream(...);
    
    s.CopyTo(outputStream);
    

    ... it will do it using a small memory buffer, and automatically.

    The same goes with Encryption, just in reverse.


    Darin R.

    Monday, March 31, 2014 4:24 PM