// save http binary content response in a file (dont use character encoding, just save bytes) public bool LoadToFile(string file, bool exportRequest = false) { bool ret = false; //FileStream fs = null; if (__trace) { pb.Trace.WriteLine("Http.LoadToFile()"); } zfile.CreateFileDirectory(file); //fs = new FileStream(file, FileMode.Create, FileAccess.Write, FileShare.Read); Action action = () => { StreamTransfer streamTransfer = new StreamTransfer(); streamTransfer.SourceLength = _resultContentLength; ///////////////////streamTransfer.Progress.ProgressChanged += new Progress.ProgressChangedEventHandler(StreamTransferProgressChange); //using (FileStream fs = new FileStream(file, FileMode.Create, FileAccess.Write, FileShare.Read)) using (FileStream fs = zFile.Open(file, FileMode.Create, FileAccess.Write, FileShare.Read)) ret = streamTransfer.Transfer(GetResponseStream(), fs); SetRequestDuration(DateTime.Now - RequestTime); //if (exportRequest) // ExportRequest(file); }; Action retry = () => { //FileStream fs2 = fs; //fs = null; //fs2.Close(); //fs = new FileStream(file, FileMode.Create, FileAccess.Write, FileShare.Read); }; Action final = () => { //if (fs != null) // fs.Close(); if (exportRequest) { _ExportRequest(file); } }; Try(action, retry, final); return(ret); }
public string ProcessExecute(string fileName) { string outFile = TempNameGenerator.GenerateTempNameFromFile(fileName); using (FileStream outFileStream = new FileStream(outFile, FileMode.Create, FileAccess.Write)) { using (ZOutputStream outZStream = new ZOutputStream(outFileStream, zlibConst.Z_BEST_COMPRESSION)) { using (FileStream inFileStream = new System.IO.FileStream(fileName, FileMode.Open, FileAccess.Read)) { transfer.Transfer(inFileStream, outZStream); } } } return(outFile); }
public void Compress(CompressorOption option) { bool ignore_all = false; PrimeArchiverType type = PrimeArchiverType.Nothing; processors.Clear(); processors.Add(new CompressFile()); if (!String.IsNullOrEmpty(option.Password)) { type |= PrimeArchiverType.Password; EncryptFile ef = new EncryptFile(); ef.SetKey(option.Password); processors.Add(ef); } header = new Header.Header(); HeaderItemPath hip = new HeaderItemPath(); try { string temp_archive = TempNameGenerator.GenerateTempNameFromFile(option.Output); using (FileStream archiveStream = new FileStream(temp_archive, FileMode.Create, FileAccess.Write)) { IncludesPathCreate(option); for (int i = 0; i < header.Items.Count; i++) { HeaderItemPath hip_file = new HeaderItemPath(); try { Process(header.Items[i].AbsolutePath); if (header.Items[i].Length != 0) { hip_file.UpdateCurrentPath(header.Items[i].AbsolutePath); foreach (IProcessFile processor in processors) { hip_file.UpdateCurrentPath(processor.ProcessExecute(hip_file.GetCurrentPath())); } using (FileStream fr = new FileStream(hip_file.GetCurrentPath(), FileMode.Open, FileAccess.Read)) { header.Items[i].SetLentgh(fr.Length); transfer.Transfer(fr, archiveStream); } } } catch (Exception ex) { if (ignore_all) { continue; } OperationErrorAction action = OperationErrorAction.Abort; switch (action) { case OperationErrorAction.Abort: throw ex; case OperationErrorAction.Ignore: continue; case OperationErrorAction.IgnoreAll: ignore_all = true; continue; case OperationErrorAction.Replay: i--; break; } } finally { hip_file.ClearTemporeryPathes(false); } } } string header_path = TempNameGenerator.GenerateTempNameFromFile(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "header.dat")); hip.UpdateCurrentPath(header_path); using (FileStream fs = new FileStream(hip.GetCurrentPath(), FileMode.Create, FileAccess.Write)) { new StreamTransfer().Transfer(new MemoryStream(header.ToArray()), fs); } foreach (IProcessFile processor in processors) { hip.UpdateCurrentPath(processor.ProcessExecute(hip.GetCurrentPath())); } using (FileStream endArchiveStream = new FileStream(option.Output, FileMode.Create, FileAccess.Write)) { using (FileStream fr = new FileStream(hip.GetCurrentPath(), FileMode.Open, FileAccess.Read)) { endArchiveStream.WriteByte((byte)type); int after_processors_header_length = (int)fr.Length; endArchiveStream.Write(BitConverter.GetBytes(after_processors_header_length), 0, sizeof(int)); transfer.Transfer(fr, endArchiveStream); } using (FileStream fr = new FileStream(temp_archive, FileMode.Open, FileAccess.Read)) { transfer.Transfer(fr, endArchiveStream); } Operations.DeleteFile(temp_archive); } } catch (Exception ex) { Process(ex.Message); } finally { hip.ClearTemporeryPathes(true); processors.Clear(); } }
public bool LoadToFile(string path) { bool ret = false; FileStream fs = null; try { Open(); string dir = Path.GetDirectoryName(path); if (!Directory.Exists(dir)) Directory.CreateDirectory(dir); fs = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Read); DateTime dtFirstCatch = new DateTime(0); while (true) { try { //cu.StreamWrite(gStream, fs); if (gAbortTransfer) { ret = false; break; } //gStreamTransfer = new StreamTransfer(gProgress.ProgressControl); gStreamTransfer = new StreamTransfer(); gStreamTransfer.SourceLength = gContentLength; gStreamTransfer.Progress.ProgressChanged += new Progress.ProgressChangedEventHandler(StreamTransferProgressChange); ret = gStreamTransfer.Transfer(gStream, fs); break; } catch (Exception ex) { if (ex is IOException) throw; if (ex is ThreadAbortException) throw; if (giLoadXmlRetryTimeout == 0) throw; if (dtFirstCatch.Ticks == 0) { dtFirstCatch = DateTime.Now; } else if (giLoadXmlRetryTimeout != -1) { dtFirstCatch = DateTime.Now; TimeSpan ts = DateTime.Now.Subtract(dtFirstCatch); if (ts.Seconds > giLoadXmlRetryTimeout) throw; } if (HttpRetry != null && !HttpRetry(ex)) throw; Close(); Open(); FileStream fs2 = fs; fs = null; fs2.Close(); fs = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Read); } } } finally { gAbortTransfer = false; if (fs != null) fs.Close(); Close(); } return ret; }
public bool LoadToFile(string file, bool exportRequest = false) { bool ret = false; FileStream fs = null; try { if (__trace) { pb.Trace.WriteLine("Http.LoadToFile()"); } Open(); zfile.CreateFileDirectory(file); //fs = new FileStream(file, FileMode.Create, FileAccess.Write, FileShare.Read); fs = zFile.Open(file, FileMode.Create, FileAccess.Write, FileShare.Read); //DateTime dtFirstCatch = new DateTime(0); DateTime dtFirstCatch = DateTime.Now; while (true) { try { //if (_abortTransfer) //{ // ret = false; // break; //} StreamTransfer streamTransfer = new StreamTransfer(); streamTransfer.SourceLength = _resultContentLength; streamTransfer.Progress.ProgressChanged += new Progress.ProgressChangedEventHandler(StreamTransferProgressChange); ret = streamTransfer.Transfer(_stream, fs); _webRequestDuration = DateTime.Now - _webRequestTime; if (exportRequest) { ExportRequest(file); } break; } catch (Exception ex) { if (ex is IOException) { throw; } if (ex is ThreadAbortException) { throw; } if (_loadRetryTimeout == 0) { throw; } if (dtFirstCatch.Ticks == 0) { dtFirstCatch = DateTime.Now; } else if (_loadRetryTimeout != -1) { dtFirstCatch = DateTime.Now; TimeSpan ts = DateTime.Now.Subtract(dtFirstCatch); if (ts.Seconds > _loadRetryTimeout) { throw; } } //if (HttpRetry != null && !HttpRetry(ex)) // throw; Close(); Open(); FileStream fs2 = fs; fs = null; fs2.Close(); //fs = new FileStream(file, FileMode.Create, FileAccess.Write, FileShare.Read); fs = zFile.Open(file, FileMode.Create, FileAccess.Write, FileShare.Read); } } } finally { //_abortTransfer = false; if (fs != null) { fs.Close(); } Close(); } return(ret); }