private static FileDatabaseManifest DeserializeManifest( byte[] encryptedBytes, Config config ) { using (MemoryStream deserializedMemoryStream = new MemoryStream()) { using (MemoryStream compressedBytesStream = new MemoryStream(EncryptionHelpers.DecryptBytes(encryptedBytes, config.EncryptionKey, config.InitializationVector))) using (GZipStream decompressionStream = new GZipStream(compressedBytesStream, CompressionMode.Decompress)) { decompressionStream.CopyTo(deserializedMemoryStream); } return(JsonConvert.DeserializeObject <FileDatabaseManifest>( Encoding.UTF8.GetString(deserializedMemoryStream.ToArray()) )); } }
private void ReconstructFile( string destination, IEnumerable <Tuple <string, long> > localFileShardIDPathsAndIndices ) { long currentShard = 0; using (FileStream outputFileStream = System.IO.File.Create(destination)) { this.Info("Piecing file together"); foreach (string fileShardPath in localFileShardIDPathsAndIndices.OrderBy(t => t.Item2).Select(t => t.Item1)) { this.Verbose($"Adding shard: {currentShard}"); // Load the bytes into memory byte[] decryptedBytes = EncryptionHelpers.DecryptBytes( System.IO.File.ReadAllBytes(fileShardPath), _config.EncryptionKey, _config.InitializationVector ); // Load the string FileShard deserializedFileShard = JsonConvert.DeserializeObject <FileShard>( Encoding.UTF8.GetString(decryptedBytes, 0, decryptedBytes.Length) ); // Assert that the current shard equals the one we expect if (deserializedFileShard.PieceNumber != currentShard) { throw new InvalidOperationException("The file shard IDs are out of order!"); } outputFileStream.Write(deserializedFileShard.Payload, 0, deserializedFileShard.Payload.Length); currentShard++; System.IO.File.Delete(fileShardPath); } } }