ZForge.Zip.ZipDirEntry.Read C# (CSharp) Method

Read() public static method

Reads one entry from the zip directory structure in the zip file.
public static Read ( System s ) : ZipDirEntry
s System the stream from which to read.
return ZipDirEntry
        public static ZipDirEntry Read(System.IO.Stream s)
        {
            int signature = ZForge.Zip.Shared.ReadSignature(s);
            // return null if this is not a local file header signature
            if (ZipDirEntry.IsNotValidSig(signature))
            {
                s.Seek(-4, System.IO.SeekOrigin.Current);

                // Getting "not a ZipDirEntry signature" here is not always wrong or an error.
                // This can happen when walking through a zipfile.  After the last ZipDirEntry,
                // we expect to read an EndOfCentralDirectorySignature.  When we get this is how we
                // know we've reached the end of the central directory.
                if (signature != ZipConstants.EndOfCentralDirectorySignature)
                {
                   throw new Exception(String.Format("  ZipDirEntry::Read(): Bad signature ({0:X8}) at position 0x{1:X8}", signature, s.Position));
                }
                return null;
            }

            byte[] block = new byte[42];
            int n = s.Read(block, 0, block.Length);
            if (n != block.Length) return null;

            int i = 0;
            ZipDirEntry zde = new ZipDirEntry();

            zde._VersionMadeBy = (short)(block[i++] + block[i++] * 256);
            zde._VersionNeeded = (short)(block[i++] + block[i++] * 256);
            zde._BitField = (short)(block[i++] + block[i++] * 256);
            zde._CompressionMethod = (short)(block[i++] + block[i++] * 256);
            zde._LastModDateTime = block[i++] + block[i++] * 256 + block[i++] * 256 * 256 + block[i++] * 256 * 256 * 256;
            zde._Crc32 = block[i++] + block[i++] * 256 + block[i++] * 256 * 256 + block[i++] * 256 * 256 * 256;
            zde._CompressedSize = block[i++] + block[i++] * 256 + block[i++] * 256 * 256 + block[i++] * 256 * 256 * 256;
            zde._UncompressedSize = block[i++] + block[i++] * 256 + block[i++] * 256 * 256 + block[i++] * 256 * 256 * 256;

            zde._LastModified = ZForge.Zip.Shared.PackedToDateTime(zde._LastModDateTime);

            Int16 filenameLength = (short)(block[i++] + block[i++] * 256);
            Int16 extraFieldLength = (short)(block[i++] + block[i++] * 256);
            Int16 commentLength = (short)(block[i++] + block[i++] * 256);
            Int16 diskNumber = (short)(block[i++] + block[i++] * 256);
            Int16 internalFileAttrs = (short)(block[i++] + block[i++] * 256);
            Int32 externalFileAttrs = block[i++] + block[i++] * 256 + block[i++] * 256 * 256 + block[i++] * 256 * 256 * 256;
            Int32 Offset = block[i++] + block[i++] * 256 + block[i++] * 256 * 256 + block[i++] * 256 * 256 * 256;

            block = new byte[filenameLength];
            n = s.Read(block, 0, block.Length);
            zde._FileName = ZForge.Zip.Shared.StringFromBuffer(block, 0, block.Length);

            zde._Extra = new byte[extraFieldLength];
            n = s.Read(zde._Extra, 0, zde._Extra.Length);

            block = new byte[commentLength];
            n = s.Read(block, 0, block.Length);
            zde._Comment = ZForge.Zip.Shared.StringFromBuffer(block, 0, block.Length);

            return zde;
        }

Usage Example

Beispiel #1
0
        private static void ReadIntoInstance(ZipFile zf)
        {
            zf._entries = new System.Collections.Generic.List <ZipEntry>();
            ZipEntry e;

            if (zf.Verbose)
            {
                zf.Output.WriteLine("Reading zip {0}...", zf.Name);
            }

            while ((e = ZipEntry.Read(zf.ReadStream)) != null)
            {
                if (zf.Verbose)
                {
                    zf.Output.WriteLine("  {0}", e.FileName);
                }

                if (zf._Debug)
                {
                    System.Console.WriteLine("  ZipFile::Read(): ZipEntry: {0}", e.FileName);
                }

                zf._entries.Add(e);
            }

            // read the zipfile's central directory structure here.
            zf._direntries = new System.Collections.Generic.List <ZipDirEntry>();

            ZipDirEntry de;

            while ((de = ZipDirEntry.Read(zf.ReadStream)) != null)
            {
                if (zf._Debug)
                {
                    System.Console.WriteLine("  ZipFile::Read(): ZipDirEntry: {0}", de.FileName);
                }
                zf._direntries.Add(de);
            }

            // when finished slurping in the zip, close the read stream
            zf.ReadStream.Close();
            zf.ReadStream = null;
        }