Unique class for compression/decompression file. Represents a Zip file. FULL CREDITS GO TO : ZipStorer, by Jaime Olivares Website: http://github.com/jaime-olivares/zipstorer Version: 2.35 (March 14, 2010)
상속: IDisposable
예제 #1
0
        /// <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);
        }
예제 #2
0
파일: ZipStorer.cs 프로젝트: devjerome/3P
        /// <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();
        }