public void AddFile(DeduplicatorState state, FileInfo sourceFile, string destinationPath) { if (state.DestinationToFileHash.ContainsKey(destinationPath)) { // File has already been added. return; } // Read the source file. var memory = new MemoryStream(); using (var stream = new BufferedStream(new FileStream(sourceFile.FullName, FileMode.Open, FileAccess.Read, FileShare.None), 1200000)) { stream.CopyTo(memory); } // Hash the memory stream. var sha1 = new SHA1Managed(); memory.Seek(0, SeekOrigin.Begin); var hashBytes = sha1.ComputeHash(memory); var hashString = BitConverter.ToString(hashBytes).Replace("-", "").ToLowerInvariant(); memory.Seek(0, SeekOrigin.Begin); // Add to the file hash -> source map if not already present. if (!state.FileHashToSource.ContainsKey(hashString)) { state.FileHashToSource.Add(hashString, memory); } else { memory.Dispose(); } state.DestinationToFileHash.Add(destinationPath, hashString); }
private static void SendFile(string path, HttpListenerResponse target) { target.ContentLength64 = new FileInfo(path).Length; using (var file = new BufferedStream(File.OpenRead(path))) using (var output = target.OutputStream) { file.CopyTo(output); } }
public MemoryStream Load(string path) { var result = new MemoryStream(); using (var fileStream = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) using (var bufferedStream = new BufferedStream(fileStream)) { bufferedStream.CopyTo(result); } return result; }
public static byte[] Decompress(byte[] inputData) { if (inputData == null) throw new ArgumentNullException("inputData"); using (var compressedMs = new MemoryStream(inputData)) { using (var decompressedMs = MiNetServer.MemoryStreamManager.GetStream()) { using (var gzs = new BufferedStream(new GZipStream(compressedMs, CompressionMode.Decompress), 2*4096)) { gzs.CopyTo(decompressedMs); } return decompressedMs.ToArray(); } } }
public static byte[] Decompress(byte[] inputData) { if (inputData == null) throw new ArgumentNullException("inputData must be non-null"); using (var compressedMs = new MemoryStream(inputData)) { using (var decompressedMs = new MemoryStream()) { using (var gzs = new BufferedStream(new GZipStream(compressedMs, CompressionMode.Decompress), BUFFER_SIZE)) { gzs.CopyTo(decompressedMs); } return decompressedMs.ToArray(); } } }
/// <summary> /// Creates a zip archive containing specific files from the application folder /// </summary> /// <param name="appPath">The path to the application folder</param> /// <param name="files">The files that will be added to the archive.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns>An open stream of the zip file</returns> public async Task<System.IO.Stream> GetZippedPayload(string appPath, IEnumerable<string> files, System.Threading.CancellationToken cancellationToken) { string zipFile = Path.Combine(Path.GetTempPath(), "payload.zip"); //// If no files need to be uploaded we create a dummy file using (var enumerator = files.GetEnumerator()) { //// equivalent to ( files.Count == 0 ) if (enumerator.MoveNext() == false) { string emptyFile = "_empty_"; File.WriteAllText(Path.Combine(appPath, emptyFile), Guid.NewGuid().ToString()); files = new string[] { emptyFile }; } } using (Stream zipStream = new FileStream(zipFile, FileMode.Create)) { using (ZipArchive archive = new ZipArchive(zipStream, ZipArchiveMode.Create)) { foreach (string file in files) { if (file.Length > 0) { await Task.Factory.StartNew(new Action(() => { using (FileStream fs = new FileStream(Path.Combine(appPath, file), FileMode.Open)) { using (BufferedStream bs = new BufferedStream(fs)) { ZipArchiveEntry entry = archive.CreateEntry(file, CompressionLevel.Optimal); using (BufferedStream writer = new BufferedStream(entry.Open())) { bs.CopyTo(writer); } } } })); } } } } Stream fileStream = new FileStream(zipFile, FileMode.Open); return fileStream; }
private void toolStripItemPush_Click(object sender, EventArgs e) { if (!PromptSave()) return; if (localFileName == null || localFileName == "") { MessageBox.Show(this, "Unable to upload: No file open", "Unable to upload", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } using (var fileStream = new FileStream(localFileName, FileMode.Open, FileAccess.Read)) using (var bufferedStream = new BufferedStream(fileStream)) { try { using (var ftpStream = ftpClient.OpenWrite(this.remoteFileName, FtpDataType.ASCII)) { bufferedStream.CopyTo(ftpStream); } } catch (Exception ex) { MessageBox.Show(this, "FTP Error: " + ex.Message, "FTP Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } }