Пример #1
0
        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();
                }
            }
        }
Пример #2
0
        /// <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 stZipStorer Open(string _filename, FileAccess _access)
        {
            Stream stream = (Stream) new FileStream(_filename, FileMode.Open, _access == FileAccess.Read ? FileAccess.Read : FileAccess.ReadWrite);

            stZipStorer zip = Open(stream, _access);

            zip.FileName = _filename;

            return(zip);
        }
Пример #3
0
        /// <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 stZipStorer Create(Stream _stream, string _comment)
        {
            stZipStorer zip = new stZipStorer();

            zip.Comment       = _comment;
            zip.ZipFileStream = _stream;
            zip.Access        = FileAccess.Write;

            return(zip);
        }
Пример #4
0
        /// <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);
        }
Пример #5
0
        /// <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 stZipStorer Create(string _filename, string _comment)
        {
            Stream stream = new FileStream(_filename, FileMode.Create, FileAccess.ReadWrite);

            stZipStorer zip = Create(stream, _comment);

            zip.Comment  = _comment;
            zip.FileName = _filename;

            return(zip);
        }
Пример #6
0
        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();
                }
            }
        }
Пример #7
0
        /// <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 stZipStorer Open(Stream _stream, FileAccess _access)
        {
            if (!_stream.CanSeek && _access != FileAccess.Read)
            {
                throw new InvalidOperationException("Stream cannot seek");
            }

            stZipStorer zip = new stZipStorer();

            //zip.FileName = _filename;
            zip.ZipFileStream = _stream;
            zip.Access        = _access;

            if (zip.ReadFileInfo())
            {
                return(zip);
            }

            throw new System.IO.InvalidDataException();
        }