Unique class for compression/decompression file. Represents a Zip file.
Inheritance: IDisposable
Esempio n. 1
0
 public String ExtractFileToString(ZipStorer.ZipFileEntry zipFileEntry)
 {
     String rv = null;
     MemoryStream ms = new MemoryStream();
     if (Zip.ExtractFile(zipFileEntry, ms))
     {
         ms.Position = 0;
         rv = new StreamReader(ms).ReadToEnd().Trim();
     }
     ms.Close();
     ms.Dispose();
     return rv;
 }
Esempio n. 2
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 ZipStorer Open(Stream _stream, FileAccess _access)
        {
            if (!_stream.CanSeek && _access != FileAccess.Read)
            {
                throw new InvalidOperationException("Stream cannot seek");
            }

            ZipStorer zip = new ZipStorer();

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

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

            throw new System.IO.InvalidDataException();
        }
        /// <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");
            }

            var zip = new ZipStorer();

            //zip.FileName = _filename;
            zip._zipFileStream = stream;
            zip._access        = access;

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

            throw new InvalidDataException();
        }
Esempio n. 4
0
 public void AddStream(ZipStorer.CompressionMethod compressionMethod, Stream sourceStream, string fileNameInZip, DateTime modificationTimeStamp, string fileEntryComment)
 {
     if (this.access == FileAccess.Read)
     {
         throw new InvalidOperationException("Writing is not allowed");
     }
     ZipStorer.ZipFileEntry item = default(ZipStorer.ZipFileEntry);
     item.Method        = compressionMethod;
     item.EncodeUTF8    = this.EncodeUtf8;
     item.FilenameInZip = ZipStorer.NormalizeFileName(fileNameInZip);
     item.Comment       = ((fileEntryComment == null) ? string.Empty : fileEntryComment);
     item.Crc32         = 0u;
     item.HeaderOffset  = (uint)this.zipFileStream.Position;
     item.ModifyTime    = modificationTimeStamp;
     this.WriteLocalHeader(ref item);
     item.FileOffset = (uint)this.zipFileStream.Position;
     this.Store(ref item, sourceStream);
     sourceStream.Close();
     this.UpdateCrcAndSizes(ref item);
     this.files.Add(item);
 }
Esempio n. 5
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 ZipStorer Open(Stream _stream, FileAccess _access)
        {
            if (!_stream.CanSeek && _access != FileAccess.Read)
            {
                throw new InvalidOperationException("Stream cannot seek");
            }

            ZipStorer zip = new ZipStorer();

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

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

            //throw new System.IO.InvalidDataException();
            return(null); // TODO Unity had an issue with the above line so returning null
        }
Esempio n. 6
0
        /// <summary>
        /// Method to open an existing storage from stream
        /// </summary>
        /// <param name="stream">Already opened stream with zip contents</param>
        /// <param name="fileFileAccess">File access mode for stream operations</param>
        /// <returns>A valid ZipStorer object</returns>
        public static ZipStorer Open([NotNull] Stream stream, FileAccess fileFileAccess)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }
            if (!stream.CanSeek && fileFileAccess != FileAccess.Read)
            {
                throw new InvalidOperationException("Stream cannot seek");
            }

            ZipStorer zip = new ZipStorer {
                zipFileStream = stream, access = fileFileAccess
            };

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

            throw new InvalidDataException();
        }
Esempio n. 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>
        /// <param name="_leaveOpen">true to leave the stream open after the ZipStorer object is disposed; otherwise, false (default).</param>
        /// <returns>A valid ZipStorer object</returns>
        public static ZipStorer Open(Stream _stream, FileAccess _access, Boolean _leaveOpen = false)
        {
            if (!_stream.CanSeek && _access != FileAccess.Read)
            {
                throw new InvalidOperationException("Stream cannot seek");
            }

            ZipStorer zip = new ZipStorer
            {
                ZipFileStream = _stream,
                Access        = _access,
                leaveOpen     = _leaveOpen
            };

            //zip.FileName = _filename;

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

            throw new InvalidDataException();
        }
Esempio n. 8
0
        //ディレクトリをzipファイルに書き込む
        private static void WriteDirToZip(ZipStorer zip, DirectoryInfo srcDir, string pathInZip)
        {
            var files = srcDir.EnumerateFiles();
            files = files.Where(e => e.Name != "mimetype"); //mimetypeファイルを除く

            foreach (var file in files)
            {
                var ext = file.Extension;

                ZipStorer.Compression compression;
                //ファイル形式によって圧縮形式を変える
                switch (ext)
                {
                    case "jpg": //画像ファイルは圧縮しない(時間の無駄なので)
                    case "JPEG":
                    case "png":
                    case "PNG":
                    case "gif":
                    case "GIF":
                        compression = ZipStorer.Compression.Store;
                        break;
                    case "EPUB":
                    case "epub":
                        continue;   //EPUBファイルは格納しない
                    default:
                        compression = ZipStorer.Compression.Deflate;  //通常のファイルは圧縮する
                        break;
                }
                WriteFileToZip(zip,file, pathInZip + file.Name, compression);
            }
            //残りのディレクトリを再帰的に書き込む
            var dirs = srcDir.EnumerateDirectories();
            foreach (var dir in dirs)
            {
                WriteDirToZip(zip, dir, pathInZip + dir.Name + "/");
            }
        }
Esempio n. 9
0
 void PrepareUpdate()
 {
   updatePackage = ZipStorer.Open(updateLocation, System.IO.FileAccess.Read);
   updatePackageCatalog = updatePackage.ReadCentralDir();
 }
Esempio n. 10
0
        /// <summary>
        /// Method to create a new zip storage in a stream
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="fileComment"></param>
        /// <returns>A valid ZipStorer object</returns>
        public static ZipStorer Create(Stream stream, string fileComment)
        {
            ZipStorer zip = new ZipStorer { comment = fileComment, zipFileStream = stream, access = FileAccess.Write };

            return zip;
        }
Esempio n. 11
0
 public bool ExtractFileToStream(ZipStorer.ZipFileEntry zipFileEntry, ref MemoryStream s)
 {
     if (Zip.ExtractFile(zipFileEntry, s))
         return true;
     return false;
 }
Esempio n. 12
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></returns>
        public static ZipStorer Create(string _filename, string _comment)
        {
            ZipStorer zip = new ZipStorer();
            zip.FileName = _filename;
            zip.Comment = _comment;
            zip.ZipFileStream = new FileStream(_filename, FileMode.Create, FileAccess.ReadWrite);
            zip.Access = FileAccess.Write;

            return zip;
        }
Esempio n. 13
0
        /// <summary>
        /// Create a new zip storage in a stream.
        /// </summary>
        /// <param name="zipStream">The stream to use to create the Zip file.</param>
        /// <param name="fileComment">General comment for Zip file.</param>
        /// <returns>A valid ZipStorer object.</returns>
        public static ZipStorer Create(Stream zipStream, string fileComment)
        {
            ZipStorer zip = new ZipStorer();
            zip.comment = fileComment;
            zip.zipFileStream = zipStream;
            zip.access = FileAccess.Write;

            return zip;
        }
Esempio n. 14
0
 public   List<string> words = new List<string>();                                                                       // The XLSX dictionary
 public   OoXml(string template_filename)                                                                                // Constructor from a file name
 {
     zip = ZipStorer.Open(template_filename, FileAccess.Read);                                                           // Open the template
     foreach (ZipStorer.ZipFileEntry l in zip.ReadCentralDir()) streams.Add(new gStream(this, zip, l));                  // Get the streams that make up the template and add them
     SetStructure();                                                                                                     // Analyzes the Sheets structure
 }
Esempio n. 15
0
        //Epubにファイルを追加する(mimetypeを除く)
        private static void WriteEpubFilesToZip(ZipStorer zip,string srcDir)
        {
            var files = Directory.GetFiles(srcDir, "*", SearchOption.AllDirectories);           //全ファイル
            var targetFiles = files.Where(e => Path.GetFileName(e).Equals("mimetype") != true)  //mimetypeを除く
                .Select(e => new FileInfo(e));

            foreach (var targetFile in targetFiles)
            {
                var ext = targetFile.Extension;
                var compression = new ZipStorer.Compression();
                switch (ext)
                {
                    case "jpg": //画像ファイルは圧縮しない(時間の無駄なので)
                    case "JPEG":
                    case "png":
                    case "PNG":
                    case "gif":
                    case "GIF":
                        compression = ZipStorer.Compression.Store;
                        break;
                    case "EPUB":
                    case "epub":
                        continue;   //EPUBファイルは格納しない
                    default:
                        compression = ZipStorer.Compression.Deflate;  //通常のファイルは圧縮する
                        break;
                }
                //対象を書き込む
                using (var ms = new MemoryStream(File.ReadAllBytes(targetFile.FullName)))
                {
                    ms.Position = 0;    //ファイルの先頭からコピー
                    var fileNameInZip = GetRelPath(targetFile.FullName, srcDir);    //zip内でのファイル名
                    zip.AddStream(compression, fileNameInZip, ms, DateTime.Now, string.Empty);
                }
            }
        }
Esempio n. 16
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([NotNull] ref ZipStorer zip, [NotNull] List <ZipFileEntry> zfes)
        {
            if (zip == null)
            {
                throw new ArgumentNullException("zip");
            }
            if (zfes == null)
            {
                throw new ArgumentNullException("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 {
                ZipStorer tempZip = 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();

                if (File.Exists(zip.fileName))
                {
                    File.Replace(tempZipName, zip.fileName, null, true);
                }
                else
                {
                    File.Move(tempZipName, zip.fileName);
                }

                zip = 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);
        }
Esempio n. 17
0
 public PakkedFile(string name, string handle, int index, ZipStorer.ZipFileEntry entry)
 {
     Name = name;
     Handle = handle;
     IsPakked = true;
     PakIndex = index;
     Entry = entry;
 }
Esempio n. 18
0
        /// <summary>
        /// 
        /// </summary>
        private void SyncToDisk()
        {
            //[i] Sync file to disk and check zix lot size
            if (_zixLots.Count > 0)
            {
                var zlLatest = _zixLots.Last();

                //[i] Need to get new file storage.
                var zlFileSize = (int)new FileInfo(zlLatest.FilePath ).Length;
                zlLatest.FileSize = zlFileSize;

                //[i] Check zix lot file size
                if (zlLatest.FileSize < (ZixSizeMax * 1024 * 1000))
                {

                    var zlFstream = new FileStream(zlLatest.FilePath,FileMode.OpenOrCreate,FileAccess.ReadWrite,FileShare.ReadWrite );
                    _zixStorer = ZipStorer.Open(zlFstream, FileAccess.ReadWrite);
                    _zixLot = zlLatest;
                }
                else
                {
                    var zlNewSeq = _zixLots.Count + 1;
                    var zlNewFileName = _zixStorageName + "." + ZixLotFileExt + "." + zlNewSeq ;
                    var zlNew = new ZixLot
                    {
                        FileName = zlNewFileName,
                        FilePath = _zixStoragePath + zlNewFileName,
                        FileSize = 0,
                        LotSequence = _zixLots.Count + 1
                    };

                    _zixStorer = ZipStorer.Create(_zixStoragePath + zlNew.FileName, "engine:db:zix");
                    _zixLots.Add(zlNew);
                    _zixLot = zlNew;
                }

            }
            else
            {
                var zlNewFileName = _zixStorageName + "." + ZixLotFileExt + ".1";
                var zlNew = new ZixLot
                {
                    FileName = zlNewFileName,
                    FilePath = _zixStoragePath + zlNewFileName,
                    FileSize = 0,
                    LotSequence = 1
                };

                _zixStorer = ZipStorer.Create(zlNew.FilePath, "engine:db:zix");
                _zixLots.Add(zlNew);
                _zixLot = zlNew;
            }
        }
Esempio n. 19
0
 public PakFile(string name, string handle)
 {
     Handle = handle;
     Name = name;
     Storer = ZipStorer.Open(handle, FileAccess.Read);
 }
Esempio n. 20
0
 public void Open()
 {
     Zip = ZipStorer.Open(zipFile, FileAccess.Read);
 }
Esempio n. 21
0
 public void Save()
 {
     Zip = ZipStorer.Create(zipFile, "GamePower Package");
     Zip.EncodeUTF8 = false;
     foreach (String file in Files)
         Zip.AddFile(ZipStorer.Compression.Store, file, Path.GetFileName(file), String.Empty);
 }
Esempio n. 22
0
        /// <summary>
        /// Method to open an existing storage from stream
        /// </summary>
        /// <param name="stream">Already opened stream with zip contents</param>
        /// <param name="fileFileAccess">File access mode for stream operations</param>
        /// <returns>A valid ZipStorer object</returns>
        public static ZipStorer Open( [NotNull] Stream stream, FileAccess fileFileAccess ) {
            if( stream == null ) throw new ArgumentNullException( "stream" );
            if( !stream.CanSeek && fileFileAccess != FileAccess.Read )
                throw new InvalidOperationException( "Stream cannot seek" );

            ZipStorer zip = new ZipStorer { zipFileStream = stream, access = fileFileAccess };

            if( zip.ReadFileInfo() )
                return zip;

            throw new InvalidDataException();
        }
Esempio n. 23
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 ZipStorer Open(Stream _stream, FileAccess _access)
        {
            if (!_stream.CanSeek && _access != FileAccess.Read)
                 throw new InvalidOperationException("Stream cannot seek");

             ZipStorer zip = new ZipStorer();
             //zip.FileName = _filename;
             zip.ZipFileStream = _stream;
             zip.Access = _access;

             if (zip.ReadFileInfo())
                 return zip;

             throw new System.IO.InvalidDataException();
        }
Esempio n. 24
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 ZipStorer zip, [NotNull] List<ZipFileEntry> zfes ) {
            if( zfes == null ) throw new ArgumentNullException( "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 {
                ZipStorer tempZip = 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();

                if( File.Exists( zip.fileName ) ) {
                    File.Replace( tempZipName, zip.fileName, null, true );
                } else {
                    File.Move( tempZipName, zip.fileName );
                }

                zip = 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;
        }
Esempio n. 25
0
        void AddCritterType( ZipStorer zip, CritterType crType )
        {
            object datafile = null;
            if( !OpenCurrentDatafile( ref datafile ) )
                return;

            List<CritterAnimationPacked> zipFiles = new List<CritterAnimationPacked>();

            foreach( CritterAnimation crAnim in crType.Animations )
            {
                List<string> nameList = new List<string>();
                List<byte[]> bytesList = new List<byte[]>();
                List<DateTime> dateList = new List<DateTime>();

                string crName = crType.Name + crAnim.Name;

                for( int d = 0; d <= 5; d++ )
                {
                    if( crAnim.Dir[d] == CritterAnimationDir.None )
                        continue;

                    string ext = ".FR" + (crAnim.Full ? "M" : d.ToString());

                    switch( LoadedMode )
                    {
                        case LoadModeType.Directory:
                            string filename = openDirectory.SelectedPath + Path.DirectorySeparatorChar + crName + ext;
                            if( File.Exists( filename ) )
                            {
                                zipFiles.Add( new CritterAnimationPacked(
                                    ArtCrittersZip + crName + ext,
                                    File.ReadAllBytes( filename ),
                                    File.GetLastWriteTime( filename )
                                 ) );
                            }
                            break;

                        case LoadModeType.Zip:
                            ZipStorer zipDatafile = (ZipStorer)datafile;
                            MemoryStream stream = new MemoryStream();
                            zipDatafile.ExtractFile( crAnim.ZipData[d], stream );
                            zipFiles.Add( new CritterAnimationPacked(
                                ArtCrittersZip + crName + ext,
                                stream.ToArray(),
                                crAnim.ZipData[d].ModifyTime ) );
                            break;

                        case LoadModeType.Dat:
                            DAT dat = (DAT)datafile;
                            zipFiles.Add( new CritterAnimationPacked(
                                ArtCrittersZip + crName + ext,
                                dat.FileList[crAnim.DatData[d]].GetData(),
                                DateTime.Now ) );
                            break;
                    }

                    if( crAnim.Full )
                        break;
                }
            }

            CloseCurrentDatafile( ref datafile );

            foreach( CritterAnimationPacked crAnimPacked in zipFiles )
            {
                MemoryStream stream = new MemoryStream( crAnimPacked.Bytes, false );
                zip.AddStream( ZipStorer.Compression.Deflate, crAnimPacked.Filename, stream, crAnimPacked.Date, "" );
            }
        }
Esempio n. 26
0
        public static ZipStorer Open(Stream stream, FileAccess access)
        {
            if (!stream.CanSeek && access != FileAccess.Read)
            {
                throw new InvalidOperationException("Stream cannot seek");
            }

            ZipStorer zip = new ZipStorer();
            zip.zipFileStream = stream;
            zip.access = access;

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

            throw new System.IO.InvalidDataException();
        }
Esempio n. 27
0
        public static ZipStorer Create([NotNull] Stream stream, [NotNull] string fileComment) {
            if (stream == null) throw new ArgumentNullException("stream");
            if (fileComment == null) throw new ArgumentNullException("fileComment");
            ZipStorer zip = new ZipStorer {
                comment = fileComment,
                zipFileStream = stream,
                access = FileAccess.Write
            };

            return zip;
        }
Esempio n. 28
0
        public  gSheet Sheet = null;                                                                                            // This links to a data sheet if his stream implements a data sheet

        public gStream(OoXml doc, ZipStorer zip, ZipStorer.ZipFileEntry z)                                                      // This constructor is called when creating the stream from the source template
        {
            Document = doc;                                                                                                     // Save a reference to the document  
            zfe = z;                                                                                                            // Store the ZipFileEntry  
        }
Esempio n. 29
0
        /// <summary>
        /// Method to open an existing storage
        /// </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></returns>
        public static ZipStorer Open(string _filename, FileAccess _access)
        {
            ZipStorer zip = new ZipStorer();
            zip.FileName = _filename;
            zip.ZipFileStream = new FileStream(_filename, FileMode.Open, _access == FileAccess.Read ? FileAccess.Read : FileAccess.ReadWrite);
            zip.Access = _access;

            if (zip.ReadFileInfo())
                return zip;

            throw new System.IO.InvalidDataException();
        }
Esempio n. 30
0
 public void Close()                                                                                                     // Close the excel file
 {
     if (zip != null) { zip.Close(); zip = null; }
 }
Esempio n. 31
0
 public bool ExtractFile(ZipStorer.ZipFileEntry zipFileEntry, String filename)
 {
     return Zip.ExtractFile(zipFileEntry, filename);
 }
Esempio n. 32
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 ZipStorer Create(Stream _stream, string _comment)
        {
            ZipStorer zip = new ZipStorer();
             zip.Comment = _comment;
             zip.ZipFileStream = _stream;
             zip.Access = FileAccess.Write;

             return zip;
        }
Esempio n. 33
0
 //ファイルをzipファイルに書き込む
 private static void WriteFileToZip(ZipStorer zip, FileInfo file, string fileNameInZip, ZipStorer.Compression compression)
 {
     using(var m =new MemoryStream(File.ReadAllBytes(file.FullName)))    //対象をファイルから読み出す
     {
         m.Position = 0; //先頭からコピー
         zip.AddStream(compression, fileNameInZip, m, DateTime.Now, String.Empty);   //zipファイルに格納する
     }
 }
Esempio n. 34
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 ZipStorer _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
             {
                 ZipStorer tempZip = ZipStorer.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 = ZipStorer.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;
        }
Esempio n. 35
0
        void AddCritterType( ZipStorer zip, string crTypeName )
        {
            CritterType crType = CritterTypes.Find( cr => crTypeName.ToUpper() == cr.Name );
            if( crType == null )
            {
                return;
            }

            AddCritterType( zip, crType );
        }