internal static void DownloadToLocation(Uri WebLocation, string fileLocation, int timeout) { ShFileUtils.DeleteOrRename(fileLocation); // create all folders to the file Directory.CreateDirectory(Path.GetDirectoryName(fileLocation)); // This was the old way to download file // unfortunatelly it doesn't have an easy way to change the default read timeout //using (WebClient client = new WebClient()) //{ // client.DownloadFile(WebLocation, fileLocation); //} using (var webClient = new WebClient()) using (var stream = webClient.OpenRead(WebLocation)) { if (stream != null) { using (var file = File.OpenWrite(fileLocation)) { // If we had use .net 4 framework, all the following lines could be replaced by: // stream.CopyTo(file) var buffer = new byte[1024]; int bytesReceived = 0; stream.ReadTimeout = timeout; while ((bytesReceived = stream.Read(buffer, 0, buffer.Length)) != 0) { file.Write(buffer, 0, bytesReceived); } } } } }
public static void ExtractZipFile(string archiveFilenameIn, string password, string outFolder) { ZipFile zf = null; try { FileStream fs = File.OpenRead(archiveFilenameIn); zf = new ZipFile(fs); if (!String.IsNullOrEmpty(password)) { zf.Password = password; // AES encrypted entries are handled automatically } foreach (ZipEntry zipEntry in zf) { if (!zipEntry.IsFile) { continue; // Ignore directories } String entryFileName = zipEntry.Name; // to remove the folder from the entry:- entryFileName = Path.GetFileName(entryFileName); // Optionally match entrynames against a selection list here to skip as desired. // The unpacked length is available in the zipEntry.Size property. byte[] buffer = new byte[4096]; // 4K is optimum Stream zipStream = zf.GetInputStream(zipEntry); // Manipulate the output filename here as desired. String fullZipToPath = Path.Combine(outFolder, entryFileName); string directoryName = Path.GetDirectoryName(fullZipToPath); if (directoryName.Length > 0) { Directory.CreateDirectory(directoryName); } ShFileUtils.DeleteOrRename(fullZipToPath); // Unzip file in buffered chunks. This is just as fast as unpacking to a buffer the full size // of the file, but does not waste memory. // The "using" will close the stream even if an exception occurs. using (FileStream streamWriter = File.Create(fullZipToPath)) { StreamUtils.Copy(zipStream, streamWriter, buffer); } } } finally { if (zf != null) { zf.IsStreamOwner = true; // Makes close also shut the underlying stream zf.Close(); // Ensure we release resources } } }