private string __ParseZip(string fPath) { stCore.stZipStorer zip = null; try { if (!File.Exists(fPath)) { this.Fire_ProcessLog( new RevErrorEventArgs( new Exception(Properties.Resources.txtZipNotFound), MethodBase.GetCurrentMethod().Name ) ); return(String.Empty); } zip = stZipStorer.Open(fPath, System.IO.FileAccess.Read); foreach (stZipStorer.ZipFileEntry entry in (List <stZipStorer.ZipFileEntry>)zip.ReadCentralDir()) { #if SHOW_PROGRESSBAR this._progressBar(++_pbCounter); #endif if (Path.GetFileName(entry.FilenameInZip).Equals(_watchList[1])) { string opath = Path.Combine( _rootPath, _watchList[1] ); if (!Directory.Exists(Path.GetDirectoryName(opath))) { Directory.CreateDirectory(Path.GetDirectoryName(opath)); } __delTmpFileName(opath); zip.ExtractFile(entry, opath); return(opath); } } Exception e = new Exception(Properties.Resources.txtZipEntryNotFound); this.Fire_ProcessLog(new RevErrorEventArgs(e, MethodBase.GetCurrentMethod().Name)); return(String.Empty); } catch (Exception e) { this.Fire_ProcessLog(new RevErrorEventArgs(e, MethodBase.GetCurrentMethod().Name)); return(String.Empty); } finally { if (zip != null) { zip.Close(); zip.Dispose(); } } }
/// <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 stZipStorer _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 { stZipStorer tempZip = stZipStorer.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 = stZipStorer.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); }
public static bool unzipMaxmindData(string path, string zipName, string archName) { stCore.stZipStorer zip = null; try { string pathSource = Path.Combine( path, zipName ); if (!File.Exists(pathSource)) { throw new FileNotFoundException( string.Format( Properties.Resources.GeoMaxMindFileNotFound, zipName ) ); } zip = stCore.stZipStorer.Open(pathSource, System.IO.FileAccess.Read); foreach (stCore.stZipStorer.ZipFileEntry entry in (List <stCore.stZipStorer.ZipFileEntry>)zip.ReadCentralDir()) { if (Path.GetFileName(entry.FilenameInZip).Equals(archName)) { pathSource = Path.Combine( path, archName ); zip.ExtractFile(entry, pathSource); return(true); } } return(false); } catch (Exception e) { throw new ArgumentException(e.Message); } finally { if (zip != null) { zip.Close(); zip.Dispose(); } } }