コード例 #1
0
 /// <summary>
 /// Unserialize zip 
 /// </summary>
 /// <param name="input">input stream</param>
 /// <returns>operation outcome</returns>
 public bool UnSerialize()
 {
     try
     {
         /// Start at the beginning of the EOF
         this.m_zipReader.Seek(-0x36, SeekOrigin.End);
         /// Get central directory info from the eof header
         CentralDirectoryRecordEOF eof = new CentralDirectoryRecordEOF();
         eof.UnSerialize(this.m_zipReader);
         this.m_lZipDirectory = new List<ZipFile>(eof.TotalCDirEntries);
         ///Seek to the beginning of the zip to read the preload local file header
         //this.m_zipReader.Seek(0, SeekOrigin.Begin);
         //LocalFileHeader preloadHdr = new LocalFileHeader();
         //preloadHdr.UnSerialize(this.m_zipReader);
         //m_preloadSection = new PreloadSection(new MemoryStream(preloadHdr.FileData));
         /// Read preload
         //Thread preloadWorker = new Thread(new ThreadStart(m_preloadSection.UnSerialize));
         //preloadWorker.Start();
         ///Parse Central Directory
         this.m_zipReader.Seek((int)eof.StartOfCDir, SeekOrigin.Begin);
         ZipFile file;
         CentralDirectoryEntry entry;
         for (int i = 1; i < eof.TotalCDirEntries; i++)
         {
             entry = new CentralDirectoryEntry(this.m_zipReader);
             file = new ZipFile(entry, i);
             this.m_lZipDirectory.Add(file);
         }
         GetGame();
         return true;
     }
     catch {  return false; }
 }
コード例 #2
0
        /// <summary>
        /// Saves the current zip file
        /// </summary>
        /// <param name="output">location to save</param>
        /// <returns>outcome of operation</returns>
        public bool Save(string output,bool getFileData)
        {
            try
            {
                CentralDirectoryRecordEOF eof = new CentralDirectoryRecordEOF();
                eof.TotalCDirEntries = (ushort)this.m_lZipDirectory.Count;

                /// Get all file data
                if (getFileData)
                    for (int i = 0; i < this.m_lZipDirectory.Count; i++)
                        this.m_lZipDirectory[i].ChildEntry.FileData = GetFileData(i);
                using (IOWriter writer = new IOWriter(File.Create(output)))
                {
                    /// Write local file directories
                    foreach (ZipFile file in this.m_lZipDirectory)
                        writer.Write(file.CreateHeader((int)writer.BaseStream.Position).Serialize());

                    /// Pad the beginning of the central directory
                    int cdirStart = (int)writer.BaseStream.Position.Align(0x800);
                    writer.Write(new byte[cdirStart - (int)writer.BaseStream.Position]);
                    eof.StartOfCDir = (uint)cdirStart;

                    /// Write Central Directory entires
                    foreach (ZipFile file in this.m_lZipDirectory)
                        writer.Write(file.ChildEntry.Serialize());

                    /// Pad out the end of the central directory
                    int cdirEnd = (int)writer.BaseStream.Position.Align(0x800);
                    writer.Write(new byte[cdirEnd - (int)writer.BaseStream.Position]);
                    eof.SizeOfCDir = (ushort)(cdirEnd - cdirStart);
                    /// Pad zip
                    if (writer.BaseStream.Length < (int)this.m_gameName)
                        writer.Write(new byte[((int)this.m_gameName - writer.BaseStream.Length) - 54]);
                    /// Write EOF
                    writer.Write(eof.Serialize());
                }

                return true;
            }
            catch { return false; }
        }