Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Your code will be easier to debug if you break it down into smaller chunks. In my example, I have provided a Compress and Decompress method. In addition, you do not need to roll your own code to read all bytes out of a <code>FileStream</code>. You can simply use <code>File.ReadAllBytes</code>. Third, make sure you wrap classes that implement <code>IDisposable</code> in a <code>using</code> statement.</p> <pre><code>public void Execute() { isExecuting = true; byte[] data; byte[] cmpData; //create temp zip file OnMessage("Reading file to memory"); byte[] data = File.ReadAllBytes( PathToFile ); OnMessage("Zipping file to memory"); byte[] compressedData = Compress(data); OnMessage("Saving file to database"); SaveToDatabase( compressedData ); OnMessage("File Saved"); isExecuting = false; } private void SaveToDatabase( byte[] data ) { using ( var cmd = Conn.CreateCommand() ) { cmd.CommandText = @"MergeFileUploads"; cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@File", data ); cmd.Parameters["@File"].DbType = DbType.Binary; cmd.Parameters.Add("@SourceField"); var parameter = cmd.Parameters["@SourceField"]; parameter.DbType = DbType.Int32; parameter.Direction = ParameterDirection.Output; cmd.ExecuteNonQuery(); sourceFileId = (int)parameter.Value; } } private static byte[] Compress( byte[] data ) { var output = new MemoryStream(); using ( var gzip = new GZipStream( output, CompressionMode.Compress, true ) ) { gzip.Write( data, 0, data.Length ); gzip.Close(); } return output.ToArray(); } private static byte[] Decompress( byte[] data ) { var output = new MemoryStream(); var input = new MemoryStream(); input.Write( data, 0, data.Length ); input.Position = 0; using ( var gzip = new GZipStream( input, CompressionMode.Decompress, true ) ) { var buff = new byte[64]; var read = gzip.Read( buff, 0, buff.Length ); while ( read &gt; 0 ) { output.Write( buff, 0, read ); read = gzip.Read( buff, 0, buff.Length ); } gzip.Close(); } return output.ToArray(); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload