/// <summary> /// This method is used to compress the .bak file using SharpZipLib library /// </summary> /// <param name="bkFileName">.bak file to be compressed to .zip</param> /// <returns></returns> public bool CompressDbBackup() { try { string zipFilePath = FilePath.Replace(".bak", ".zip"); using (ZipOutputStream zipStream = new ZipOutputStream(File.Create(zipFilePath))) { //Compression level 0-9 (9 is highest) zipStream.SetLevel(4); //Add an entry to our zip file ZipEntry entry = new ZipEntry(Path.GetFileName(FilePath)); entry.DateTime = DateTime.Now; zipStream.Password = String.Format(Password); zipStream.PutNextEntry(entry); //to check double _sofarReadSize = 0; byte[] buffer = new byte[4096]; int byteCount = 0; double totalByte; using (FileStream inputStream = File.OpenRead(FilePath)) { byteCount = inputStream.Read(buffer, 0, buffer.Length); totalByte = inputStream.Length; _sofarReadSize += byteCount; while (byteCount > 0) { zipStream.Write(buffer, 0, byteCount); int progressPercentage = (int)((_sofarReadSize / totalByte) * 100); byteCount = inputStream.Read(buffer, 0, buffer.Length); _sofarReadSize += byteCount; UpdateCompletionStatus.ShowCompletionStatus(progressPercentage, "File compression in progress..."); } } } File.Delete(FilePath); return(true); } catch (Exception ex) { return(false); throw new TargetInvocationException(ex); } }
/// <summary> /// Shows the percentage of work completed when backuping/restoring /// </summary> /// <returns></returns> public void PercentComplete(object sender, PercentCompleteEventArgs e) { try { string _message = TaskIsBackup == true ? "Database backup in progress..." : "Database restore in progress..."; UpdateCompletionStatus.ShowCompletionStatus(e.Percent, _message); CurrentProgress = e.Percent; Console.WriteLine(e.Percent); } catch (Exception ex) { throw new TargetInvocationException(ex); } }
/// <summary> /// Extracts the .zip file /// </summary> /// <param name="zipFileName">zip file to be extracted </param> /// <returns></returns> public string ExtractZipFile(string zipFileName) { ZipFile zf = null; string fullZipToPath = String.Empty; string outputFolder = Path.GetDirectoryName(zipFileName); try { FileStream fs = File.OpenRead(zipFileName); zf = new ZipFile(fs); zf.Password = Password; //GetServerPassword(DbName); int byteCount = 0; double totalByte; double _sofarReadSize = 0; foreach (ZipEntry zipEntry in zf) { if (!zipEntry.IsFile) { continue; // Ignore directories } String entryFileName = zipEntry.Name; byte[] buffer = new byte[4096]; // 4K is optimum Stream zipStream = zf.GetInputStream(zipEntry); // Manipulate the output filename here as desired. fullZipToPath = Path.Combine(outputFolder, entryFileName); string directoryName = Path.GetDirectoryName(fullZipToPath); if (directoryName.Length > 0) { Directory.CreateDirectory(directoryName); } using (FileStream streamWriter = File.Create(fullZipToPath)) { byteCount = streamWriter.Read(buffer, 0, buffer.Length); totalByte = streamWriter.Length; _sofarReadSize += byteCount; StreamUtils.Copy(zipStream, streamWriter, buffer); int progressPercentage = (int)((_sofarReadSize / totalByte) * 100); UpdateCompletionStatus.ShowCompletionStatus(progressPercentage, "Extracting file in progress..."); } } } finally { if (zf != null) { zf.IsStreamOwner = true; zf.Close(); } } return(fullZipToPath); //extracted file path }