Exemple #1
0
        private static void ReadIntoInstance(ZipFile zf)
        {
            lock (zf)
            {
                try
                {
                    ZipEntry    e;
                    ZipDirEntry de;
                    zf.InitializeEntries(false);
                    if (zf.Verbose)
                    {
                        if (zf.Name == null)
                        {
                            zf.StatusMessageTextWriter.WriteLine("Reading zip from stream...");
                        }
                        else
                        {
                            zf.StatusMessageTextWriter.WriteLine("Reading zip {0}...", zf.Name);
                        }
                    }
                    while ((e = ZipEntry.Read(zf.ReadStream, zf.Encoding)) != null)
                    {
                        if (zf.Verbose)
                        {
                            zf.StatusMessageTextWriter.WriteLine("  {0}", e.FileName);
                        }
                        zf._entries.Add(e);
                    }

                    zf.IndexEntriesAndTrackChangesSinceNow();
                    var index = zf._entriesIndex;

                    zf._direntries = new List <ZipDirEntry>();
                    while ((de = ZipDirEntry.Read(zf.ReadStream, zf.Encoding)) != null)
                    {
                        zf._direntries.Add(de);

                        if (index.ContainsKey(de.FileName))
                        {
                            var e1 = index[de.FileName];

                            e1.Comment = de.Comment;
                            if (de.IsDirectory)
                            {
                                e1.MarkAsDirectory();
                            }
                        }
                    }
                    ReadCentralDirectoryFooter(zf);
                    if (!(!zf.Verbose || String.IsNullOrEmpty(zf.Comment)))
                    {
                        zf.StatusMessageTextWriter.WriteLine("Zip file Comment: {0}", zf.Comment);
                    }
                }
                catch (Exception e1)
                {
                    if (zf._ReadStreamIsOurs && (zf._readstream != null))
                    {
                        try
                        {
                            zf._readstream.Close();
                            zf._readstream.Dispose();
                            zf._readstream = null;
                        }
                        finally
                        {
                        }
                    }
                    throw new ZipException("Exception while reading", e1);
                }
            }
        }
Exemple #2
0
 public static ZipDirEntry Read(Stream s, Encoding expectedEncoding)
 {
     int signature = ZipSharedUtilities.ReadSignature(s);
     if (IsNotValidSig(signature))
     {
         s.Seek(-4L, SeekOrigin.Current);
         if (signature != 0x6054b50L)
         {
             throw new BadReadException(String.Format("  ZipDirEntry::Read(): Bad signature ({0:X8}) at position 0x{1:X8}", signature, s.Position));
         }
         return null;
     }
     byte[] block = new byte[0x2a];
     if (s.Read(block, 0, block.Length) != block.Length)
     {
         return null;
     }
     int i = 0;
     ZipDirEntry zde = new ZipDirEntry();
     short versionMadeBy = (short) (block[i++] + (block[i++] * 0x100));
     short versionNeeded = (short) (block[i++] + (block[i++] * 0x100));
     short bitField = (short) (block[i++] + (block[i++] * 0x100));
     short compressionMethod = (short) (block[i++] + (block[i++] * 0x100));
     int lastModDateTime = ((block[i++] + (block[i++] * 0x100)) + ((block[i++] * 0x100) * 0x100)) + (((block[i++] * 0x100) * 0x100) * 0x100);
     int crc32 = ((block[i++] + (block[i++] * 0x100)) + ((block[i++] * 0x100) * 0x100)) + (((block[i++] * 0x100) * 0x100) * 0x100);
     int compressedSize = ((block[i++] + (block[i++] * 0x100)) + ((block[i++] * 0x100) * 0x100)) + (((block[i++] * 0x100) * 0x100) * 0x100);
     int uncompressedSize = ((block[i++] + (block[i++] * 0x100)) + ((block[i++] * 0x100) * 0x100)) + (((block[i++] * 0x100) * 0x100) * 0x100);
     short filenameLength = (short) (block[i++] + (block[i++] * 0x100));
     short extraFieldLength = (short) (block[i++] + (block[i++] * 0x100));
     short commentLength = (short) (block[i++] + (block[i++] * 0x100));
     i += 2;
     zde._InternalFileAttrs = (short) (block[i++] + (block[i++] * 0x100));
     zde._ExternalFileAttrs = ((block[i++] + (block[i++] * 0x100)) + ((block[i++] * 0x100) * 0x100)) + (((block[i++] * 0x100) * 0x100) * 0x100);
     block = new byte[filenameLength];
     int n = s.Read(block, 0, block.Length);
     if ((bitField & 0x800) == 0x800)
     {
         zde._FileName = ZipSharedUtilities.Utf8StringFromBuffer(block, block.Length);
     }
     else
     {
         zde._FileName = ZipSharedUtilities.StringFromBuffer(block, block.Length, expectedEncoding);
     }
     if (extraFieldLength > 0)
     {
         zde._Extra = new byte[extraFieldLength];
         n = s.Read(zde._Extra, 0, zde._Extra.Length);
     }
     if (commentLength > 0)
     {
         block = new byte[commentLength];
         n = s.Read(block, 0, block.Length);
         if ((bitField & 0x800) == 0x800)
         {
             zde._Comment = ZipSharedUtilities.Utf8StringFromBuffer(block, block.Length);
         }
         else
         {
             zde._Comment = ZipSharedUtilities.StringFromBuffer(block, block.Length, expectedEncoding);
         }
     }
     return zde;
 }
Exemple #3
0
        private static bool ReadHeader(ZipEntry ze, Encoding defaultEncoding)
        {
            // no seek - stream integrity should be protected by the caller

            int bytesRead = 0;

            ze._RelativeOffsetOfHeader = (int)ze.ArchiveStream.Position;
            int signature = ZipSharedUtilities.ReadSignature(ze.ArchiveStream);

            bytesRead += 4;
            if (IsNotValidSig(signature))
            {
                ze.ArchiveStream.Seek(-4L, SeekOrigin.Current);
                if (ZipDirEntry.IsNotValidSig(signature) && (signature != 0x6054b50L))
                {
                    throw new BadReadException(String.Format("  ZipEntry::ReadHeader(): Bad signature (0x{0:X8}) at position  0x{1:X8}", signature, ze.ArchiveStream.Position));
                }
                return(false);
            }
            byte[] block = new byte[0x1a];
            int    n     = ze.ArchiveStream.Read(block, 0, block.Length);

            if (n != block.Length)
            {
                return(false);
            }
            bytesRead += n;
            int i = 0;

            ze._VersionNeeded     = (short)(block[i++] + (block[i++] * 0x100));
            ze._BitField          = (short)(block[i++] + (block[i++] * 0x100));
            ze._CompressionMethod = (short)(block[i++] + (block[i++] * 0x100));
            ze._TimeBlob          = ((block[i++] + (block[i++] * 0x100)) + ((block[i++] * 0x100) * 0x100)) + (((block[i++] * 0x100) * 0x100) * 0x100);
            ze._LastModified      = ZipSharedUtilities.PackedToDateTime(ze._TimeBlob);
            if ((ze._BitField & 8) != 8)
            {
                ze._Crc32            = ((block[i++] + (block[i++] * 0x100)) + ((block[i++] * 0x100) * 0x100)) + (((block[i++] * 0x100) * 0x100) * 0x100);
                ze._CompressedSize   = ((block[i++] + (block[i++] * 0x100)) + ((block[i++] * 0x100) * 0x100)) + (((block[i++] * 0x100) * 0x100) * 0x100);
                ze._UncompressedSize = ((block[i++] + (block[i++] * 0x100)) + ((block[i++] * 0x100) * 0x100)) + (((block[i++] * 0x100) * 0x100) * 0x100);
            }
            else
            {
                i += 12;
            }
            short filenameLength   = (short)(block[i++] + (block[i++] * 0x100));
            short extraFieldLength = (short)(block[i++] + (block[i++] * 0x100));

            block      = new byte[filenameLength];
            n          = ze.ArchiveStream.Read(block, 0, block.Length);
            bytesRead += n;

            var s1 = ZipSharedUtilities.StringFromBuffer(block, block.Length, Encoding.GetEncoding(1251));
            var s2 = ZipSharedUtilities.StringFromBuffer(block, block.Length, Encoding.ASCII);
            var s3 = ZipSharedUtilities.StringFromBuffer(block, block.Length, Encoding.UTF8);
            var s4 = ZipSharedUtilities.StringFromBuffer(block, block.Length, Encoding.Default);
            var s5 = ZipSharedUtilities.StringFromBuffer(block, block.Length, Encoding.GetEncoding(1252));

            if ((ze._BitField & 0x800) == 0x800)
            {
                ze._FileNameInArchive = ZipSharedUtilities.StringFromBuffer(block, block.Length, Encoding.UTF8);
                ze.UseUtf8Encoding    = true;
            }
            else
            {
                ze._FileNameInArchive = ZipSharedUtilities.StringFromBuffer(block, block.Length, defaultEncoding);
                ze._encoding          = defaultEncoding;
            }
            ze._LocalFileName = ze._FileNameInArchive;
            if (extraFieldLength > 0)
            {
                ze._Extra  = new byte[extraFieldLength];
                n          = ze.ArchiveStream.Read(ze._Extra, 0, ze._Extra.Length);
                bytesRead += n;
            }
            if (!ze.FileName.EndsWith("/") && ((ze._BitField & 8) == 8))
            {
                long posn           = ze.ArchiveStream.Position;
                bool wantMore       = true;
                long SizeOfDataRead = 0L;
                int  tries          = 0;
                while (wantMore)
                {
                    tries++;
                    long d = ZipSharedUtilities.FindSignature(ze.ArchiveStream, 0x8074b50);
                    if (d == -1L)
                    {
                        return(false);
                    }
                    SizeOfDataRead += d;
                    block           = new byte[12];
                    n = ze.ArchiveStream.Read(block, 0, block.Length);
                    if (n != 12)
                    {
                        return(false);
                    }
                    bytesRead           += n;
                    i                    = 0;
                    ze._Crc32            = ((block[i++] + (block[i++] * 0x100)) + ((block[i++] * 0x100) * 0x100)) + (((block[i++] * 0x100) * 0x100) * 0x100);
                    ze._CompressedSize   = ((block[i++] + (block[i++] * 0x100)) + ((block[i++] * 0x100) * 0x100)) + (((block[i++] * 0x100) * 0x100) * 0x100);
                    ze._UncompressedSize = ((block[i++] + (block[i++] * 0x100)) + ((block[i++] * 0x100) * 0x100)) + (((block[i++] * 0x100) * 0x100) * 0x100);
                    if (SizeOfDataRead != ze._CompressedSize)
                    {
                        ze.ArchiveStream.Seek(-12L, SeekOrigin.Current);
                        SizeOfDataRead += 4L;
                    }
                }
                ze.ArchiveStream.Seek(posn, SeekOrigin.Begin);
            }
            ze._CompressedFileDataSize = ze._CompressedSize;
            if ((ze._BitField & 1) == 1)
            {
                throw new NotSupportedException("Encryption is not supported");
            }
            ze._TotalEntrySize = bytesRead + ze._CompressedFileDataSize;
            ze._LengthOfHeader = bytesRead;
            return(true);
        }
Exemple #4
0
        public static ZipDirEntry Read(Stream s, Encoding expectedEncoding)
        {
            int signature = ZipSharedUtilities.ReadSignature(s);

            if (IsNotValidSig(signature))
            {
                s.Seek(-4L, SeekOrigin.Current);
                if (signature != 0x6054b50L)
                {
                    throw new BadReadException(String.Format("  ZipDirEntry::Read(): Bad signature ({0:X8}) at position 0x{1:X8}", signature, s.Position));
                }
                return(null);
            }
            byte[] block = new byte[0x2a];
            if (s.Read(block, 0, block.Length) != block.Length)
            {
                return(null);
            }
            int         i                 = 0;
            ZipDirEntry zde               = new ZipDirEntry();
            short       versionMadeBy     = (short)(block[i++] + (block[i++] * 0x100));
            short       versionNeeded     = (short)(block[i++] + (block[i++] * 0x100));
            short       bitField          = (short)(block[i++] + (block[i++] * 0x100));
            short       compressionMethod = (short)(block[i++] + (block[i++] * 0x100));
            int         lastModDateTime   = ((block[i++] + (block[i++] * 0x100)) + ((block[i++] * 0x100) * 0x100)) + (((block[i++] * 0x100) * 0x100) * 0x100);
            int         crc32             = ((block[i++] + (block[i++] * 0x100)) + ((block[i++] * 0x100) * 0x100)) + (((block[i++] * 0x100) * 0x100) * 0x100);
            int         compressedSize    = ((block[i++] + (block[i++] * 0x100)) + ((block[i++] * 0x100) * 0x100)) + (((block[i++] * 0x100) * 0x100) * 0x100);
            int         uncompressedSize  = ((block[i++] + (block[i++] * 0x100)) + ((block[i++] * 0x100) * 0x100)) + (((block[i++] * 0x100) * 0x100) * 0x100);
            short       filenameLength    = (short)(block[i++] + (block[i++] * 0x100));
            short       extraFieldLength  = (short)(block[i++] + (block[i++] * 0x100));
            short       commentLength     = (short)(block[i++] + (block[i++] * 0x100));

            i += 2;
            zde._InternalFileAttrs = (short)(block[i++] + (block[i++] * 0x100));
            zde._ExternalFileAttrs = ((block[i++] + (block[i++] * 0x100)) + ((block[i++] * 0x100) * 0x100)) + (((block[i++] * 0x100) * 0x100) * 0x100);
            block = new byte[filenameLength];
            int n = s.Read(block, 0, block.Length);

            if ((bitField & 0x800) == 0x800)
            {
                zde._FileName = ZipSharedUtilities.Utf8StringFromBuffer(block, block.Length);
            }
            else
            {
                zde._FileName = ZipSharedUtilities.StringFromBuffer(block, block.Length, expectedEncoding);
            }
            if (extraFieldLength > 0)
            {
                zde._Extra = new byte[extraFieldLength];
                n          = s.Read(zde._Extra, 0, zde._Extra.Length);
            }
            if (commentLength > 0)
            {
                block = new byte[commentLength];
                n     = s.Read(block, 0, block.Length);
                if ((bitField & 0x800) == 0x800)
                {
                    zde._Comment = ZipSharedUtilities.Utf8StringFromBuffer(block, block.Length);
                }
                else
                {
                    zde._Comment = ZipSharedUtilities.StringFromBuffer(block, block.Length, expectedEncoding);
                }
            }
            return(zde);
        }