/// <summary> /// Adds a file to the zip /// </summary> /// <param name="fileName">input file name</param> public void AddFile(string fileName) { using (IOReader reader = new IOReader(File.OpenRead(fileName))) { byte[] fileBuf = reader.ReadAllBytes(); CentralDirectoryEntry entry = new CentralDirectoryEntry(); entry.FileName = FixupPath(fileName); entry.FileSize = (uint)fileBuf.Length; entry.FileData = fileBuf; ZipFile file = new ZipFile(entry, this.m_lZipDirectory.Count + 1); this.m_lZipDirectory.Add(file); this.m_lZipDirectory.Sort(); } }
/// <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; } }
/// <summary> /// Gets exact start of a file without the header and padding /// </summary> /// <param name="entry">central directory</param> /// <returns>offset of file data</returns> private int GetExactFileStart(CentralDirectoryEntry entry) { return (int)(entry.OffsetOfLocalFile + entry.ExtraFieldLength + 30 + entry.FileName.Length); }
/// <summary> /// Creates a zip /// </summary> /// <param name="dir">directory to construct from</param> /// <param name="output">output zip</param> public void CreateZip(string dir, string output) { string[] fileEntries = Directory.GetFiles(dir,"*.*",SearchOption.AllDirectories); CentralDirectoryEntry entry; ZipFile file; this.m_lZipDirectory = new List<ZipFile>(fileEntries.Length); foreach (string fileName in fileEntries) { using (IOReader reader = new IOReader(File.OpenRead(fileName))) { entry = new CentralDirectoryEntry(); entry.FileData = reader.ReadAllBytes(); entry.FileName = fileName.Substring(dir.Length + 1); file = new ZipFile(entry); this.m_lZipDirectory.Add(file); } } GetGame(); this.m_lZipDirectory.Sort(); Save(output, false); }