/// <summary> /// Method to open an existing storage file /// </summary> /// <param name="_filename">Full path of Zip file to open</param> /// <param name="_access">File access mode as used in FileStream constructor</param> /// <returns>A valid ZipStorer object</returns> public static cFuncionesZip Open(string _filename, FileAccess _access) { Stream stream = (Stream) new FileStream(_filename, FileMode.Open, _access == FileAccess.Read ? FileAccess.Read : FileAccess.ReadWrite); cFuncionesZip zip = Open(stream, _access); zip.FileName = _filename; return(zip); }
/// <summary> /// Method to create a new zip storage in a stream /// </summary> /// <param name="_stream"></param> /// <param name="_comment"></param> /// <returns>A valid ZipStorer object</returns> public static cFuncionesZip Create(Stream _stream, string _comment) { cFuncionesZip zip = new cFuncionesZip(); zip.Comment = _comment; zip.ZipFileStream = _stream; zip.Access = FileAccess.Write; return(zip); }
/// <summary> /// Removes one of many files in storage. It creates a new Zip file. /// </summary> /// <param name="_zip">Reference to the current Zip object</param> /// <param name="_zfes">List of Entries to remove from storage</param> /// <returns>True if success, false if not</returns> /// <remarks>This method only works for storage of type FileStream</remarks> public static bool RemoveEntries(ref cFuncionesZip _zip, List <ZipFileEntry> _zfes) { if (!(_zip.ZipFileStream is FileStream)) { throw new InvalidOperationException("RemoveEntries is allowed just over streams of type FileStream"); } //Get full list of entries List <ZipFileEntry> fullList = _zip.ReadCentralDir(); //In order to delete we need to create a copy of the zip file excluding the selected items string tempZipName = Path.GetTempFileName(); string tempEntryName = Path.GetTempFileName(); try { cFuncionesZip tempZip = cFuncionesZip.Create(tempZipName, string.Empty); foreach (ZipFileEntry zfe in fullList) { if (!_zfes.Contains(zfe)) { if (_zip.ExtractFile(zfe, tempEntryName)) { tempZip.AddFile(zfe.Method, tempEntryName, zfe.FilenameInZip, zfe.Comment); } } } _zip.Close(); tempZip.Close(); File.Delete(_zip.FileName); File.Move(tempZipName, _zip.FileName); _zip = cFuncionesZip.Open(_zip.FileName, _zip.Access); } catch { return(false); } finally { if (File.Exists(tempZipName)) { File.Delete(tempZipName); } if (File.Exists(tempEntryName)) { File.Delete(tempEntryName); } } return(true); }
/// <summary> /// Method to create a new storage file /// </summary> /// <param name="_filename">Full path of Zip file to create</param> /// <param name="_comment">General comment for Zip file</param> /// <returns>A valid ZipStorer object</returns> public static cFuncionesZip Create(string _filename, string _comment) { Stream stream = new FileStream(_filename, FileMode.Create, FileAccess.ReadWrite); cFuncionesZip zip = Create(stream, _comment); zip.Comment = _comment; zip.FileName = _filename; return(zip); }
/// <summary> /// Method to open an existing storage from stream /// </summary> /// <param name="_stream">Already opened stream with zip contents</param> /// <param name="_access">File access mode for stream operations</param> /// <returns>A valid ZipStorer object</returns> public static cFuncionesZip Open(Stream _stream, FileAccess _access) { if (!_stream.CanSeek && _access != FileAccess.Read) { throw new InvalidOperationException("Stream cannot seek"); } cFuncionesZip zip = new cFuncionesZip(); //zip.FileName = _filename; zip.ZipFileStream = _stream; zip.Access = _access; if (zip.ReadFileInfo()) { return(zip); } throw new System.IO.InvalidDataException(); }
public static bool crearBackupBD(string strServer, string strUser, string strPass, string strNameBD) { bool bProcede = false; try { if (!Directory.Exists(Application.StartupPath + "\\backups")) { Directory.CreateDirectory(Application.StartupPath + "\\backups"); } //primero creamos el archivo .Bat string strBatFile = "@echo off\r\n" + "for /f \"tokens=1-4 delims=/ \" %%i in (\"%date%\") do (\r\n" + " set day=%%i\r\n" + " set month=%%j\r\n" + " set year=%%k\r\n" + ")\r\n" + "set datestr=%day%_%month%_%year%\r\n" + "echo datestr is %datestr%\r\n" + "set BACKUP_FILE=backups\\" + strNameBD + "_%datestr%.dmp\r\n" + "echo backup file name is %BACKUP_FILE%\r\n" + "SET PGPASSWORD="******"\r\n" + "echo off\r\n" + "pg\\pg_dump -h " + strServer + " -U " + strUser + " -f %BACKUP_FILE% " + strNameBD + "\r\necho PROCESANDO....."; cFuncionesFicheros.guardarArchivo(Application.StartupPath + "\\backup.bat", strBatFile); ProcessStartInfo info = new ProcessStartInfo(); info.FileName = Application.StartupPath + "\\backup.bat"; info.CreateNoWindow = true; info.UseShellExecute = true; Process proc = new Process(); proc.StartInfo = info; proc.Start(); proc.WaitForExit(); File.Delete(Application.StartupPath + "\\backup.bat"); //Comprimimos el archivo cFuncionesZip zip = new cFuncionesZip(); string mes = (DateTime.Now.Month < 10) ? "0" + DateTime.Now.Month : DateTime.Now.Month.ToString(); string dia = (DateTime.Now.Day < 10) ? "0" + DateTime.Now.Day : DateTime.Now.Day.ToString(); string path = Application.StartupPath + "\\backups\\" + strNameBD + "_" + dia + "_" + mes + "_" + DateTime.Now.Year + ".dmp"; zip = cFuncionesZip.Create(path + ".zip", "Generado por ZipStorer para NeuroDiagnostico"); zip.AddFile(cFuncionesZip.Compression.Deflate, path, Path.GetFileName(path), ""); zip.Close(); //Si se ha creado el ZIP... se borra el backup if (File.Exists(path + ".zip")) { File.Delete(path); } bProcede = true; } catch (Exception exp) { throw exp; } return(bProcede); }