/// <summary> /// Zip the given folder /// </summary> public static bool ZipFolder(string zipPath, string folderPath, ZipStorer.Compression compressionMethod) { if (string.IsNullOrEmpty(zipPath) || string.IsNullOrEmpty(folderPath)) { return(false); } if (!Directory.Exists(folderPath)) { return(false); } bool result = true; try { ZipStorer zip; if (!File.Exists(zipPath)) { var zipFolder = Path.GetDirectoryName(zipPath); if (!CreateDirectory(zipFolder)) { return(false); } zip = ZipStorer.Create(zipPath, "Created with 3P @ " + DateTime.Now + "\r\n" + Config.UrlWebSite); } else { zip = ZipStorer.Open(zipPath, FileAccess.Write); } foreach (var file in Directory.EnumerateFiles(folderPath, "*", SearchOption.AllDirectories)) { zip.AddFile(compressionMethod, file, file.Replace(folderPath, "").TrimStart('\\'), "Added @ " + DateTime.Now); } zip.Close(); } catch (Exception e) { ErrorHandler.ShowErrors(e, "Error zipping " + folderPath); result = false; } return(result); }
/// <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 ZipStorer Open(Stream stream, FileAccess access) { if (!stream.CanSeek && access != FileAccess.Read) { throw new InvalidOperationException("Stream cannot seek"); } ZipStorer zip = new ZipStorer { _zipFileStream = stream, _access = access }; //zip.FileName = _filename; if (zip.ReadFileInfo()) { return(zip); } throw new InvalidDataException(); }