/// <summary> /// This will throw if the zipfile does not exist. /// </summary> public static ZipFile Read(string zipFileName) { if (zipFileName == null) { throw new ArgumentNullException("zipFileName"); } ZipFile zf = new ZipFile(); zf.m_FileName = zipFileName; using (Stream readStream = File.OpenRead(zipFileName)) { ZipEntry e; while ((e = ZipEntry.Read(readStream)) != null) { zf.m_Entries.Add(e); } // read the zipfile's central directory structure here. ZipDirEntry de; while ((de = ZipDirEntry.Read(readStream)) != null) { zf.m_DirEntries.Add(de); } } return(zf); }
public static ZipDirEntry Read(Stream s) { int signature = Shared.ReadSignature(s); // return null if this is not a local file header signature if (SignatureIsNotValid(signature)) { s.Seek(-4, SeekOrigin.Current); return(null); } ZipDirEntry zde = new ZipDirEntry(); BinaryReader br = new BinaryReader(s); zde.m_VersionMadeBy = br.ReadInt16(); zde.m_VersionNeeded = br.ReadInt16(); zde.m_BitField = br.ReadInt16(); zde.m_CompressionMethod = br.ReadInt16(); zde.m_LastModDateTime = br.ReadInt32(); zde.m_Crc32 = br.ReadInt32(); zde.m_CompressedSize = br.ReadInt32(); zde.m_UncompressedSize = br.ReadInt32(); zde.m_LastModified = Shared.PackedToDateTime(zde.m_LastModDateTime); Int16 fileNameLength = br.ReadInt16(); Int16 extraFieldLength = br.ReadInt16(); Int16 commentLength = br.ReadInt16(); Int16 diskNumber = br.ReadInt16(); Int16 internalFileAttrs = br.ReadInt16(); Int32 externalFileAttrs = br.ReadInt32(); Int32 offset = br.ReadInt32(); byte[] block = br.ReadBytes(fileNameLength); if (block.Length != fileNameLength) { throw new Exception("Invalid filename block."); } zde.m_FileName = Shared.StringFromBuffer(block); zde.m_Extra = br.ReadBytes(extraFieldLength); if (zde.m_Extra.Length != extraFieldLength) { throw new Exception("Invalid extra block."); } block = br.ReadBytes(commentLength); if (block.Length != commentLength) { throw new Exception("Invalid comment block."); } zde.m_Comment = Shared.StringFromBuffer(block); return(zde); }