IsCompressionMethodSupported() public method

Test entry to see if data can be extracted.
public IsCompressionMethodSupported ( ) : bool
return bool
Exemplo n.º 1
0
        private void ExtractEntry(ZipEntry entry)
        {
            bool   doExtraction = entry.IsCompressionMethodSupported();
            string targetName   = entry.Name;

            if (doExtraction)
            {
                if (entry.IsFile)
                {
                    targetName = extractNameTransform_.TransformFile(targetName);
                }
                else if (entry.IsDirectory)
                {
                    targetName = extractNameTransform_.TransformDirectory(targetName);
                }

                doExtraction = !((targetName == null) || (targetName.Length == 0));
            }

            // TODO: Fire delegate/throw exception were compression method not supported, or name is invalid?

            string dirName = null;

            if (doExtraction)
            {
                if (entry.IsDirectory)
                {
                    dirName = targetName;
                }
                else
                {
                    dirName = Path.GetDirectoryName(Path.GetFullPath(targetName));
                }
            }

            if (doExtraction && !Directory.Exists(dirName))
            {
                if (!entry.IsDirectory)
                {
                    try
                    {
                        Directory.CreateDirectory(dirName);
                    }
                    catch (Exception)
                    {
                        doExtraction     = false;
                        continueRunning_ = false;
                        throw;
                    }
                }
            }

            if (doExtraction && entry.IsFile)
            {
                ExtractFileEntry(entry, targetName);
            }
        }
Exemplo n.º 2
0
        private void ExtractEntry(ZipEntry entry)
        {
            bool   flag = entry.IsCompressionMethodSupported();
            string text = entry.Name;

            if (flag)
            {
                if (entry.IsFile)
                {
                    text = extractNameTransform_.TransformFile(text);
                }
                else if (entry.IsDirectory)
                {
                    text = extractNameTransform_.TransformDirectory(text);
                }
                flag = text != null && text.get_Length() != 0;
            }
            string text2 = null;

            if (flag)
            {
                text2 = ((!entry.IsDirectory) ? Path.GetDirectoryName(Path.GetFullPath(text)) : text);
            }
            if (flag && !Directory.Exists(text2) && (!entry.IsDirectory || CreateEmptyDirectories))
            {
                try
                {
                    Directory.CreateDirectory(text2);
                }
                catch (global::System.Exception e)
                {
                    flag = false;
                    if (events_ == null)
                    {
                        continueRunning_ = false;
                        throw;
                    }
                    if (entry.IsDirectory)
                    {
                        continueRunning_ = events_.OnDirectoryFailure(text, e);
                    }
                    else
                    {
                        continueRunning_ = events_.OnFileFailure(text, e);
                    }
                }
            }
            if (flag && entry.IsFile)
            {
                ExtractFileEntry(entry, text);
            }
        }
Exemplo n.º 3
0
        private void ExtractEntry(ZipEntry entry)
        {
            bool doExtraction = NameIsValid(entry.Name) && entry.IsCompressionMethodSupported();

            // TODO: Fire delegate were compression method not supported.

            string dirName    = null;
            string targetName = null;

            if (doExtraction)
            {
                string entryFileName;
                if (Path.IsPathRooted(entry.Name))
                {
                    string workName = Path.GetPathRoot(entry.Name);
                    workName      = entry.Name.Substring(workName.Length);
                    entryFileName = Path.Combine(Path.GetDirectoryName(workName), Path.GetFileName(entry.Name));
                }
                else
                {
                    entryFileName = entry.Name;
                }

                targetName = Path.Combine(targetDirectory_, entryFileName);
                dirName    = Path.GetDirectoryName(Path.GetFullPath(targetName));

                doExtraction = (entryFileName.Length > 0);
            }

            if (doExtraction && !Directory.Exists(dirName))
            {
                if (!entry.IsDirectory || CreateEmptyDirectories)
                {
                    try
                    {
                        Directory.CreateDirectory(dirName);
                    }
                    catch
                    {
                        doExtraction = false;
                    }
                }
            }

            if (doExtraction && entry.IsFile)
            {
                ExtractFileEntry(entry, targetName);
            }
        }
Exemplo n.º 4
0
        private void ExtractEntry(ZipEntry entry)
        {
            bool   flag = entry.IsCompressionMethodSupported();
            string name = entry.Name;

            if (flag)
            {
                if (entry.IsFile)
                {
                    name = this.extractNameTransform_.TransformFile(name);
                }
                else if (entry.IsDirectory)
                {
                    name = this.extractNameTransform_.TransformDirectory(name);
                }
                flag = (name != null) && (name.Length != 0);
            }
            string path = null;

            if (flag)
            {
                path = !entry.IsDirectory ? Path.GetDirectoryName(Path.GetFullPath(name)) : name;
            }
            if ((flag && !Directory.Exists(path)) && (!entry.IsDirectory || this.CreateEmptyDirectories))
            {
                try
                {
                    Directory.CreateDirectory(path);
                }
                catch (Exception exception)
                {
                    flag = false;
                    if (this.events_ == null)
                    {
                        this.continueRunning_ = false;
                        throw;
                    }
                    this.continueRunning_ = !entry.IsDirectory ? this.events_.OnFileFailure(name, exception) : this.events_.OnDirectoryFailure(name, exception);
                }
            }
            if (flag && entry.IsFile)
            {
                this.ExtractFileEntry(entry, name);
            }
        }
Exemplo n.º 5
0
        void ExtractEntry(ZipEntry entry)
        {
            bool doExtraction = false;

            string nameText = entry.Name;

            if (entry.IsFile)
            {
                // TODO: Translate invalid names allowing extraction still.
                doExtraction = NameIsValid(nameText) && entry.IsCompressionMethodSupported();
            }
            else if (entry.IsDirectory)
            {
                doExtraction = NameIsValid(nameText);
            }

            // TODO: Fire delegate were compression method not supported, or name is invalid?

            string dirName    = null;
            string targetName = null;

            if (doExtraction)
            {
                // Handle invalid entry names by chopping of path root.
                if (Path.IsPathRooted(nameText))
                {
                    string workName = Path.GetPathRoot(nameText);
                    nameText = nameText.Substring(workName.Length);
                }

                if (nameText.Length > 0)
                {
                    targetName = Path.Combine(targetDirectory_, nameText);
                    if (entry.IsDirectory)
                    {
                        dirName = targetName;
                    }
                    else
                    {
                        dirName = Path.GetDirectoryName(Path.GetFullPath(targetName));
                    }
                }
                else
                {
                    doExtraction = false;
                }
            }

            if (doExtraction && !Directory.Exists(dirName))
            {
                if (!entry.IsDirectory || CreateEmptyDirectories)
                {
                    try {
                        Directory.CreateDirectory(dirName);
                    }
                    catch (Exception ex) {
                        doExtraction = false;
                        if (events_ != null)
                        {
                            if (entry.IsDirectory)
                            {
                                continueRunning_ = events_.OnDirectoryFailure(targetName, ex);
                            }
                            else
                            {
                                continueRunning_ = events_.OnFileFailure(targetName, ex);
                            }
                        }
                        else
                        {
                            continueRunning_ = false;
                        }
                    }
                }
            }

            if (doExtraction && entry.IsFile)
            {
                ExtractFileEntry(entry, targetName);
            }
        }
Exemplo n.º 6
0
		private void ExtractZipEntry(ZipFile zipFile, ZipEntry entry)
		{
			if (!entry.IsCompressionMethodSupported() || string.IsNullOrEmpty(entry.Name)) return;

			string tPath = Path.Combine(_tempPath, entry.Name);
			string path = entry.IsDirectory ? tPath : Path.GetDirectoryName(Path.GetFullPath(tPath));

			if (!Directory.Exists(path))
			{
				Directory.CreateDirectory(path);
			}

			if (!entry.IsFile) return;

//			try
//			{
				using (FileStream stream = File.Create(tPath))
				{
					if (buffer == null)
					{
						buffer = new byte[0x1000];
					}

					using(Stream inputStream = zipFile.GetInputStream(entry))
					{
						int count;
						while ((count = inputStream.Read(buffer, 0, buffer.Length)) > 0)
						{
							stream.Write(buffer, 0, count);
						}
					}

					

					stream.Flush();
				}
//			}
//			catch
//			{
//				throw;
//			}
		}
Exemplo n.º 7
0
        /// <summary>
        /// Advances to the next entry in the archive
        /// </summary>
        /// <returns>
        /// The next <see cref="ZipEntry">entry</see> in the archive or null if there are no more entries.
        /// </returns>
        /// <remarks>
        /// If the previous entry is still open <see cref="CloseEntry">CloseEntry</see> is called.
        /// </remarks>
        /// <exception cref="InvalidOperationException">
        /// Input stream is closed
        /// </exception>
        /// <exception cref="ZipException">
        /// Password is not set, password is invalid, compression method is invalid,
        /// version required to extract is not supported
        /// </exception>
        public ZipEntry GetNextEntry()
        {
            if (crc == null)
            {
                throw new InvalidOperationException("Closed.");
            }

            if (entry != null)
            {
                CloseEntry();
            }

            int header = inputBuffer.ReadLeInt();

            if (header == ZipConstants.CentralHeaderSignature ||
                header == ZipConstants.EndOfCentralDirectorySignature ||
                header == ZipConstants.CentralHeaderDigitalSignature ||
                header == ZipConstants.ArchiveExtraDataSignature ||
                header == ZipConstants.Zip64CentralFileHeaderSignature)
            {
                // No more individual entries exist
#if NET45
                Close();
#endif
#if NETSTANDARD1_3
                Dispose();
#endif
                return(null);
            }

            // -jr- 07-Dec-2003 Ignore spanning temporary signatures if found
            // Spanning signature is same as descriptor signature and is untested as yet.
            if ((header == ZipConstants.SpanningTempSignature) || (header == ZipConstants.SpanningSignature))
            {
                header = inputBuffer.ReadLeInt();
            }

            if (header != ZipConstants.LocalHeaderSignature)
            {
                throw new ZipException("Wrong Local header signature: 0x" + String.Format("{0:X}", header));
            }

            var versionRequiredToExtract = (short)inputBuffer.ReadLeShort();

            flags  = inputBuffer.ReadLeShort();
            method = inputBuffer.ReadLeShort();
            var dostime = (uint)inputBuffer.ReadLeInt();
            int crc2    = inputBuffer.ReadLeInt();
            csize = inputBuffer.ReadLeInt();
            size  = inputBuffer.ReadLeInt();
            int nameLen  = inputBuffer.ReadLeShort();
            int extraLen = inputBuffer.ReadLeShort();

            bool isCrypted = (flags & 1) == 1;

            byte[] buffer = new byte[nameLen];
            inputBuffer.ReadRawBuffer(buffer);

            string name = ZipConstants.ConvertToStringExt(flags, buffer);

            entry       = new ZipEntry(name, versionRequiredToExtract);
            entry.Flags = flags;

            entry.CompressionMethod = (CompressionMethod)method;

            if ((flags & 8) == 0)
            {
                entry.Crc            = crc2 & 0xFFFFFFFFL;
                entry.Size           = size & 0xFFFFFFFFL;
                entry.CompressedSize = csize & 0xFFFFFFFFL;

                entry.CryptoCheckValue = (byte)((crc2 >> 24) & 0xff);
            }
            else
            {
                // This allows for GNU, WinZip and possibly other archives, the PKZIP spec
                // says these values are zero under these circumstances.
                if (crc2 != 0)
                {
                    entry.Crc = crc2 & 0xFFFFFFFFL;
                }

                if (size != 0)
                {
                    entry.Size = size & 0xFFFFFFFFL;
                }

                if (csize != 0)
                {
                    entry.CompressedSize = csize & 0xFFFFFFFFL;
                }

                entry.CryptoCheckValue = (byte)((dostime >> 8) & 0xff);
            }

            entry.DosTime = dostime;

            // If local header requires Zip64 is true then the extended header should contain
            // both values.

            // Handle extra data if present.  This can set/alter some fields of the entry.
            if (extraLen > 0)
            {
                byte[] extra = new byte[extraLen];
                inputBuffer.ReadRawBuffer(extra);
                entry.ExtraData = extra;
            }

            entry.ProcessExtraData(true);
            if (entry.CompressedSize >= 0)
            {
                csize = entry.CompressedSize;
            }

            if (entry.Size >= 0)
            {
                size = entry.Size;
            }

            if (method == (int)CompressionMethod.Stored && (!isCrypted && csize != size || (isCrypted && csize - ZipConstants.CryptoHeaderSize != size)))
            {
                throw new ZipException("Stored, but compressed != uncompressed");
            }

            // Determine how to handle reading of data if this is attempted.
            if (entry.IsCompressionMethodSupported())
            {
                internalReader = new ReadDataHandler(InitialRead);
            }
            else
            {
                internalReader = new ReadDataHandler(ReadingNotSupported);
            }

            return(entry);
        }
Exemplo n.º 8
0
        private void ExtractEntry(ZipEntry entry)
        {
            bool   flag = false;
            string name = entry.Name;

            if (entry.IsFile)
            {
                flag = NameIsValid(name) && entry.IsCompressionMethodSupported();
            }
            else if (entry.IsDirectory)
            {
                flag = NameIsValid(name);
            }
            string path = null;
            string str3 = null;

            if (flag)
            {
                if (Path.IsPathRooted(name))
                {
                    string pathRoot = Path.GetPathRoot(name);
                    name = name.Substring(pathRoot.Length);
                }
                if (name.Length > 0)
                {
                    str3 = Path.Combine(this.targetDirectory_, name);
                    if (entry.IsDirectory)
                    {
                        path = str3;
                    }
                    else
                    {
                        path = Path.GetDirectoryName(Path.GetFullPath(str3));
                    }
                }
                else
                {
                    flag = false;
                }
            }
            if ((flag && !Directory.Exists(path)) && (!entry.IsDirectory || this.CreateEmptyDirectories))
            {
                try
                {
                    Directory.CreateDirectory(path);
                }
                catch (Exception exception)
                {
                    flag = false;
                    if (this.events_ != null)
                    {
                        if (entry.IsDirectory)
                        {
                            this.continueRunning_ = this.events_.OnDirectoryFailure(str3, exception);
                        }
                        else
                        {
                            this.continueRunning_ = this.events_.OnFileFailure(str3, exception);
                        }
                    }
                    else
                    {
                        this.continueRunning_ = false;
                    }
                }
            }
            if (flag && entry.IsFile)
            {
                this.ExtractFileEntry(entry, str3);
            }
        }
Exemplo n.º 9
0
        private void ExtractEntry(ZipEntry entry)
        {
            bool   flag = entry.IsCompressionMethodSupported();
            string text = entry.Name;

            if (flag)
            {
                if (entry.IsFile)
                {
                    text = this.extractNameTransform_.TransformFile(text);
                }
                else if (entry.IsDirectory)
                {
                    text = this.extractNameTransform_.TransformDirectory(text);
                }
                flag = (text != null && text.Length != 0);
            }
            string path = null;

            if (flag)
            {
                if (entry.IsDirectory)
                {
                    path = text;
                }
                else
                {
                    path = Path.GetDirectoryName(Path.GetFullPath(text));
                }
            }
            if (flag && !Directory.Exists(path))
            {
                if (entry.IsDirectory)
                {
                    if (!this.CreateEmptyDirectories)
                    {
                        goto IL_D9;
                    }
                }
                try
                {
                    Directory.CreateDirectory(path);
                }
                catch (Exception e)
                {
                    flag = false;
                    if (this.events_ == null)
                    {
                        this.continueRunning_ = false;
                        throw;
                    }
                    if (entry.IsDirectory)
                    {
                        this.continueRunning_ = this.events_.OnDirectoryFailure(text, e);
                    }
                    else
                    {
                        this.continueRunning_ = this.events_.OnFileFailure(text, e);
                    }
                }
            }
IL_D9:
            if (flag && entry.IsFile)
            {
                this.ExtractFileEntry(entry, text);
            }
        }
Exemplo n.º 10
0
 private long TestLocalHeader(ZipEntry entry, HeaderTest tests)
 {
     lock (this.baseStream_)
     {
         bool flag = (tests & HeaderTest.Header) != 0;
         bool flag2 = (tests & HeaderTest.Extract) != 0;
         this.baseStream_.Seek(this.offsetOfFirstEntry + entry.Offset, SeekOrigin.Begin);
         if (this.ReadLEUint() != 0x4034b50)
         {
             throw new ZipException(string.Format("Wrong local header signature @{0:X}", this.offsetOfFirstEntry + entry.Offset));
         }
         short num = (short) this.ReadLEUshort();
         short flags = (short) this.ReadLEUshort();
         short num3 = (short) this.ReadLEUshort();
         short num4 = (short) this.ReadLEUshort();
         short num5 = (short) this.ReadLEUshort();
         uint num6 = this.ReadLEUint();
         long num7 = this.ReadLEUint();
         long num8 = this.ReadLEUint();
         int num9 = this.ReadLEUshort();
         int num10 = this.ReadLEUshort();
         byte[] buffer = new byte[num9];
         StreamUtils.ReadFully(this.baseStream_, buffer);
         byte[] buffer2 = new byte[num10];
         StreamUtils.ReadFully(this.baseStream_, buffer2);
         ZipExtraData data = new ZipExtraData(buffer2);
         if (data.Find(1))
         {
             num8 = data.ReadLong();
             num7 = data.ReadLong();
             if ((flags & 8) != 0)
             {
                 if ((num8 != -1L) && (num8 != entry.Size))
                 {
                     throw new ZipException("Size invalid for descriptor");
                 }
                 if ((num7 != -1L) && (num7 != entry.CompressedSize))
                 {
                     throw new ZipException("Compressed size invalid for descriptor");
                 }
             }
         }
         else if ((num >= 0x2d) && ((((uint) num8) == uint.MaxValue) || (((uint) num7) == uint.MaxValue)))
         {
             throw new ZipException("Required Zip64 extended information missing");
         }
         if (flag2 && entry.IsFile)
         {
             if (!entry.IsCompressionMethodSupported())
             {
                 throw new ZipException("Compression method not supported");
             }
             if ((num > 0x33) || ((num > 20) && (num < 0x2d)))
             {
                 throw new ZipException(string.Format("Version required to extract this entry not supported ({0})", num));
             }
             if ((flags & 0x3060) != 0)
             {
                 throw new ZipException("The library does not support the zip version required to extract this entry");
             }
         }
         if (flag)
         {
             if (((((num <= 0x3f) && (num != 10)) && ((num != 11) && (num != 20))) && (((num != 0x15) && (num != 0x19)) && ((num != 0x1b) && (num != 0x2d)))) && ((((num != 0x2e) && (num != 50)) && ((num != 0x33) && (num != 0x34))) && (((num != 0x3d) && (num != 0x3e)) && (num != 0x3f))))
             {
                 throw new ZipException(string.Format("Version required to extract this entry is invalid ({0})", num));
             }
             if ((flags & 0xc010) != 0)
             {
                 throw new ZipException("Reserved bit flags cannot be set.");
             }
             if (((flags & 1) != 0) && (num < 20))
             {
                 throw new ZipException(string.Format("Version required to extract this entry is too low for encryption ({0})", num));
             }
             if ((flags & 0x40) != 0)
             {
                 if ((flags & 1) == 0)
                 {
                     throw new ZipException("Strong encryption flag set but encryption flag is not set");
                 }
                 if (num < 50)
                 {
                     throw new ZipException(string.Format("Version required to extract this entry is too low for encryption ({0})", num));
                 }
             }
             if (((flags & 0x20) != 0) && (num < 0x1b))
             {
                 throw new ZipException(string.Format("Patched data requires higher version than ({0})", num));
             }
             if (flags != entry.Flags)
             {
                 throw new ZipException("Central header/local header flags mismatch");
             }
             if (entry.CompressionMethod != ((CompressionMethod) num3))
             {
                 throw new ZipException("Central header/local header compression method mismatch");
             }
             if (entry.Version != num)
             {
                 throw new ZipException("Extract version mismatch");
             }
             if (((flags & 0x40) != 0) && (num < 0x3e))
             {
                 throw new ZipException("Strong encryption flag set but version not high enough");
             }
             if (((flags & 0x2000) != 0) && ((num4 != 0) || (num5 != 0)))
             {
                 throw new ZipException("Header masked set but date/time values non-zero");
             }
             if (((flags & 8) == 0) && (num6 != ((uint) entry.Crc)))
             {
                 throw new ZipException("Central header/local header crc mismatch");
             }
             if (((num8 == 0L) && (num7 == 0L)) && (num6 != 0))
             {
                 throw new ZipException("Invalid CRC for empty entry");
             }
             if (entry.Name.Length > num9)
             {
                 throw new ZipException("File name length mismatch");
             }
             string name = ZipConstants.ConvertToStringExt(flags, buffer);
             if (name != entry.Name)
             {
                 throw new ZipException("Central header and local header file name mismatch");
             }
             if (entry.IsDirectory)
             {
                 if (num8 > 0L)
                 {
                     throw new ZipException("Directory cannot have size");
                 }
                 if (entry.IsCrypted)
                 {
                     if (num7 > 14L)
                     {
                         throw new ZipException("Directory compressed size invalid");
                     }
                 }
                 else if (num7 > 2L)
                 {
                     throw new ZipException("Directory compressed size invalid");
                 }
             }
             if (!ZipNameTransform.IsValidName(name, true))
             {
                 throw new ZipException("Name is invalid");
             }
         }
         if ((((flags & 8) == 0) || (num8 > 0L)) || (num7 > 0L))
         {
             if (num8 != entry.Size)
             {
                 throw new ZipException(string.Format("Size mismatch between central header({0}) and local header({1})", entry.Size, num8));
             }
             if (((num7 != entry.CompressedSize) && (num7 != 0xffffffffL)) && (num7 != -1L))
             {
                 throw new ZipException(string.Format("Compressed size mismatch between central header({0}) and local header({1})", entry.CompressedSize, num7));
             }
         }
         int num11 = num9 + num10;
         return (((this.offsetOfFirstEntry + entry.Offset) + 30L) + num11);
     }
 }
Exemplo n.º 11
0
        public ZipEntry GetNextEntry()
        {
            if (crc == null)
            {
                throw new InvalidOperationException("Closed.");
            }

            if (entry != null)
            {
                CloseEntry();
            }

            int header = inputBuffer.ReadLeInt();

            if (header == ZipConstants.CentralHeaderSignature ||
                header == ZipConstants.EndOfCentralDirectorySignature ||
                header == ZipConstants.CentralHeaderDigitalSignature ||
                header == ZipConstants.ArchiveExtraDataSignature ||
                header == ZipConstants.Zip64CentralFileHeaderSignature)
            {
                Close();
                return(null);
            }


            if ((header == ZipConstants.SpanningTempSignature) || (header == ZipConstants.SpanningSignature))
            {
                header = inputBuffer.ReadLeInt();
            }

            if (header != ZipConstants.LocalHeaderSignature)
            {
                throw new ZipException("Wrong Local header signature: 0x" + String.Format("{0:X}", header));
            }

            short versionRequiredToExtract = (short)inputBuffer.ReadLeShort();

            flags  = inputBuffer.ReadLeShort();
            method = inputBuffer.ReadLeShort();
            uint dostime = (uint)inputBuffer.ReadLeInt();
            int  crc2    = inputBuffer.ReadLeInt();

            csize = inputBuffer.ReadLeInt();
            size  = inputBuffer.ReadLeInt();
            int nameLen  = inputBuffer.ReadLeShort();
            int extraLen = inputBuffer.ReadLeShort();

            bool isCrypted = (flags & 1) == 1;

            byte[] buffer = new byte[nameLen];
            inputBuffer.ReadRawBuffer(buffer);

            string name = ZipConstants.ConvertToStringExt(flags, buffer);

            entry       = new ZipEntry(name, versionRequiredToExtract);
            entry.Flags = flags;

            entry.CompressionMethod = (CompressionMethod)method;

            if ((flags & 8) == 0)
            {
                entry.Crc            = crc2 & 0xFFFFFFFFL;
                entry.Size           = size & 0xFFFFFFFFL;
                entry.CompressedSize = csize & 0xFFFFFFFFL;

                entry.CryptoCheckValue = (byte)((crc2 >> 24) & 0xff);
            }
            else
            {
                if (crc2 != 0)
                {
                    entry.Crc = crc2 & 0xFFFFFFFFL;
                }

                if (size != 0)
                {
                    entry.Size = size & 0xFFFFFFFFL;
                }

                if (csize != 0)
                {
                    entry.CompressedSize = csize & 0xFFFFFFFFL;
                }

                entry.CryptoCheckValue = (byte)((dostime >> 8) & 0xff);
            }

            entry.DosTime = dostime;

            if (extraLen > 0)
            {
                byte[] extra = new byte[extraLen];
                inputBuffer.ReadRawBuffer(extra);
                entry.ExtraData = extra;
            }

            entry.ProcessExtraData(true);
            if (entry.CompressedSize >= 0)
            {
                csize = entry.CompressedSize;
            }

            if (entry.Size >= 0)
            {
                size = entry.Size;
            }

            if (method == (int)CompressionMethod.Stored && (!isCrypted && csize != size || (isCrypted && csize - ZipConstants.CryptoHeaderSize != size)))
            {
                throw new ZipException("Stored, but compressed != uncompressed");
            }

            if (entry.IsCompressionMethodSupported())
            {
                internalReader = new ReadDataHandler(InitialRead);
            }
            else
            {
                internalReader = new ReadDataHandler(ReadingNotSupported);
            }

            return(entry);
        }
Exemplo n.º 12
0
        private void ExtractZipEntry(ZipFile zipFile, ZipEntry entry)
        {
            if (!entry.IsCompressionMethodSupported() || string.IsNullOrEmpty(entry.Name) || !entry.IsFile) return;

            using (MemoryStream stream = new MemoryStream())
            {
                if (_Buffer == null)
                {
                    _Buffer = new byte[0x1000];
                }

                Stream inputStream = zipFile.GetInputStream(entry);

                int count;
                while ((count = inputStream.Read(_Buffer, 0, _Buffer.Length)) > 0)
                {
                    stream.Write(_Buffer, 0, count);
                }

                stream.Flush();
                stream.Close();

                fakeFileSystem.Add(entry.Name, stream);
            }
        }
Exemplo n.º 13
0
        private void ExtractEntry(ZipFile zipFile, ZipEntry entry, string entryName, INameTransform extractNameTransform)
        {
            if (!entry.IsCompressionMethodSupported())
                return;

            if (entry.IsFile)
                entryName = extractNameTransform.TransformFile(entryName);
            else if (entry.IsDirectory)
                entryName = extractNameTransform.TransformDirectory(entryName);

            if (!String.IsNullOrEmpty(entryName))
            {
                Directory.CreateDirectory(
                    !entry.IsDirectory ? Path.GetDirectoryName(Path.GetFullPath(entryName)) : entryName
                );

                if (entry.IsFile)
                    ExtractFileEntry(zipFile, entry, entryName);
            }
        }
Exemplo n.º 14
0
        private void ExtractZipEntry(ZipFile zipFile, ZipEntry entry)
        {
            if (!entry.IsCompressionMethodSupported() || string.IsNullOrEmpty(entry.Name)) return;

            string tPath = Path.Combine(_tempPath, entry.Name);
            string path = entry.IsDirectory ? tPath : Path.GetDirectoryName(Path.GetFullPath(tPath));

            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }

            if (!entry.IsFile) return;

            try
            {
                using (FileStream stream = File.Create(tPath))
                {
                    if (buffer == null)
                    {
                        buffer = new byte[0x1000];
                    }

                    Stream inputStream = zipFile.GetInputStream(entry);

                    int count;
                    while ((count = inputStream.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        //if (entry.ZipFileIndex == 86)
                        //{
                        //    StringBuilder sb = new StringBuilder();
                        //    for (int i = 0; i < buffer.Length; i++)
                        //    {
                        //        sb.Append(buffer[i]);
                        //    }
                        //    System.Diagnostics.Debug.WriteLine(buffer.Length.ToString() + " " + sb.ToString());
                        //}
                        stream.Write(buffer, 0, count);
                    }

                    stream.Flush();
                }
            }
            catch
            {
                throw;
            }
        }
Exemplo n.º 15
0
 private void ExtractEntry(ZipEntry entry)
 {
     bool flag = entry.IsCompressionMethodSupported();
     string name = entry.Name;
     if (flag)
     {
         if (entry.IsFile)
         {
             name = this.extractNameTransform_.TransformFile(name);
         }
         else if (entry.IsDirectory)
         {
             name = this.extractNameTransform_.TransformDirectory(name);
         }
         flag = (name != null) && (name.Length != 0);
     }
     string path = null;
     if (flag)
     {
         if (entry.IsDirectory)
         {
             path = name;
         }
         else
         {
             path = Path.GetDirectoryName(Path.GetFullPath(name));
         }
     }
     if ((flag && !Directory.Exists(path)) && (!entry.IsDirectory || this.CreateEmptyDirectories))
     {
         try
         {
             Directory.CreateDirectory(path);
         }
         catch (Exception exception)
         {
             flag = false;
             if (this.events_ != null)
             {
                 if (entry.IsDirectory)
                 {
                     this.continueRunning_ = this.events_.OnDirectoryFailure(name, exception);
                 }
                 else
                 {
                     this.continueRunning_ = this.events_.OnFileFailure(name, exception);
                 }
             }
             else
             {
                 this.continueRunning_ = false;
                 throw;
             }
         }
     }
     if (flag && entry.IsFile)
     {
         this.ExtractFileEntry(entry, name);
     }
 }
Exemplo n.º 16
0
        /// <summary>
        /// Test a local header against that provided from the central directory
        /// </summary>
        /// <param name="entry">
        /// The entry to test against
        /// </param>
        /// <param name="tests">The type of <see cref="HeaderTest">tests</see> to carry out.</param>
        /// <returns>The offset of the entries data in the file</returns>
        long TestLocalHeader(ZipEntry entry, HeaderTest tests)
        {
            lock(baseStream_)
            {
                bool testHeader = (tests & HeaderTest.Header) != 0;
                bool testData = (tests & HeaderTest.Extract) != 0;

                baseStream_.Seek(offsetOfFirstEntry + entry.Offset, SeekOrigin.Begin);
                if ((int)ReadLEUint() != ZipConstants.LocalHeaderSignature) {
                    throw new ZipException(string.Format("Wrong local header signature @{0:X}", offsetOfFirstEntry + entry.Offset));
                }

                short extractVersion = ( short )ReadLEUshort();
                short localFlags = ( short )ReadLEUshort();
                short compressionMethod = ( short )ReadLEUshort();
                short fileTime = ( short )ReadLEUshort();
                short fileDate = ( short )ReadLEUshort();
                uint crcValue = ReadLEUint();
                long size = ReadLEUint();
                long compressedSize = ReadLEUint();
                int storedNameLength = ReadLEUshort();
                int extraDataLength = ReadLEUshort();

                if ( testData ) {
                    if ( entry.IsFile ) {
                        if ( !entry.IsCompressionMethodSupported() ) {
                            throw new ZipException("Compression method not supported");
                        }

                        if ( (extractVersion > ZipConstants.VersionMadeBy)
                            || ((extractVersion > 20) && (extractVersion < ZipConstants.VersionZip64)) ) {
                            throw new ZipException(string.Format("Version required to extract this entry not supported ({0})", extractVersion));
                        }

                        if ( (localFlags & ( int )(GeneralBitFlags.Patched | GeneralBitFlags.StrongEncryption | GeneralBitFlags.EnhancedCompress | GeneralBitFlags.HeaderMasked)) != 0 ) {
                            throw new ZipException("The library does not support the zip version required to extract this entry");
                        }
                    }
                }

                if ( testHeader ) {
                    if ((extractVersion <= 63) &&	// Ignore later versions as we dont know about them..
                        (extractVersion != 10) &&
                        (extractVersion != 11) &&
                        (extractVersion != 20) &&
                        (extractVersion != 21) &&
                        (extractVersion != 25) &&
                        (extractVersion != 27) &&
                        (extractVersion != 45) &&
                        (extractVersion != 46) &&
                        (extractVersion != 50) &&
                        (extractVersion != 51) &&
                        (extractVersion != 52) &&
                        (extractVersion != 61) &&
                        (extractVersion != 62) &&
                        (extractVersion != 63)
                        ) {
                        throw new ZipException(string.Format("Version required to extract this entry is invalid ({0})", extractVersion));
                    }

                    // Local entry flags dont have reserved bit set on.
                    if ( (localFlags & ( int )(GeneralBitFlags.ReservedPKware4 | GeneralBitFlags.ReservedPkware14 | GeneralBitFlags.ReservedPkware15)) != 0 ) {
                        throw new ZipException("Reserved bit flags cannot be set.");
                    }

                    // Encryption requires extract version >= 20
                    if ( ((localFlags & ( int )GeneralBitFlags.Encrypted) != 0) && (extractVersion < 20) ) {
                        throw new ZipException(string.Format("Version required to extract this entry is too low for encryption ({0})", extractVersion));
                    }

                    // Strong encryption requires encryption flag to be set and extract version >= 50.
                    if ( (localFlags & (int)GeneralBitFlags.StrongEncryption) != 0 ) {
                        if ( (localFlags & (int)GeneralBitFlags.Encrypted) == 0 ) {
                            throw new ZipException("Strong encryption flag set but encryption flag is not set");
                        }

                        if ( extractVersion < 50 ) {
                            throw new ZipException(string.Format("Version required to extract this entry is too low for encryption ({0})", extractVersion));
                        }
                    }

                    // Patched entries require extract version >= 27
                    if ( ((localFlags & ( int )GeneralBitFlags.Patched) != 0) && (extractVersion < 27) ) {
                        throw new ZipException(string.Format("Patched data requires higher version than ({0})", extractVersion));
                    }

                    // Central header flags match local entry flags.
                    if ( localFlags != entry.Flags ) {
                        throw new ZipException("Central header/local header flags mismatch");
                    }

                    // Central header compression method matches local entry
                    if ( entry.CompressionMethod != ( CompressionMethod )compressionMethod ) {
                        throw new ZipException("Central header/local header compression method mismatch");
                    }

                    // Strong encryption and extract version match
                    if ( (localFlags & ( int )GeneralBitFlags.StrongEncryption) != 0 ) {
                        if ( extractVersion < 62 ) {
                            throw new ZipException("Strong encryption flag set but version not high enough");
                        }
                    }

                    if ( (localFlags & ( int )GeneralBitFlags.HeaderMasked) != 0 ) {
                        if ( (fileTime != 0) || (fileDate != 0) ) {
                            throw new ZipException("Header masked set but date/time values non-zero");
                        }
                    }

                    if ( (localFlags & ( int )GeneralBitFlags.Descriptor) == 0 ) {
                        if ( crcValue != (uint)entry.Crc ) {
                            throw new ZipException("Central header/local header crc mismatch");
                        }
                    }

                    // Crc valid for empty entry.
                    if ( (size == 0) && (compressedSize == 0) ) {
                        if ( crcValue != 0 ) {
                            throw new ZipException("Invalid CRC for empty entry");
                        }
                    }

                    // TODO: make test more correct...  can't compare lengths as was done originally as this can fail for MBCS strings
                    // Assuming a code page at this point is not valid?  Best is to store the name length in the ZipEntry probably
                    if ( entry.Name.Length > storedNameLength ) {
                        throw new ZipException("File name length mismatch");
                    }

                    byte[] nameData = new byte[storedNameLength];
                    StreamUtils.ReadFully(baseStream_, nameData);

                    string localName = ZipConstants.ConvertToStringExt(localFlags, nameData);

                    // Central directory and local entry name match
                    if ( localName != entry.Name ) {
                        throw new ZipException("Central header and local header file name mismatch");
                    }

                    // Directories have zero size.
                    if ( entry.IsDirectory ) {
                        if ( (compressedSize != 0) || (size != 0) ) {
                            throw new ZipException("Directory cannot have size");
                        }
                    }

                    if ( !ZipNameTransform.IsValidName(localName, true) ) {
                        throw new ZipException("Name is invalid");
                    }

                    byte[] data = new byte[extraDataLength];
                    StreamUtils.ReadFully(baseStream_, data);
                    ZipExtraData ed = new ZipExtraData(data);

                    // Extra data / zip64 checks
                    if ( ed.Find(1) ) {
                        // Zip64 extra data but 'extract version' is too low
                        if ( extractVersion < ZipConstants.VersionZip64 ) {
                            throw new ZipException(
                                string.Format("Extra data contains Zip64 information but version {0}.{1} is not high enough",
                                extractVersion / 10, extractVersion % 10));
                        }

                        // Zip64 extra data but size fields dont indicate its required.
                        if ( (( uint )size != uint.MaxValue) && (( uint )compressedSize != uint.MaxValue) ) {
                            throw new ZipException("Entry sizes not correct for Zip64");
                        }

                        size = ed.ReadLong();
                        compressedSize = ed.ReadLong();
                    }
                    else {
                        // No zip64 extra data but entry requires it.
                        if ( (extractVersion >= ZipConstants.VersionZip64) &&
                            ((( uint )size == uint.MaxValue) || (( uint )compressedSize == uint.MaxValue)) ) {
                            throw new ZipException("Required Zip64 extended information missing");
                        }
                    }
                }

                int extraLength = storedNameLength + extraDataLength;
                return offsetOfFirstEntry + entry.Offset + ZipConstants.LocalHeaderBaseSize + extraLength;
            }
        }
Exemplo n.º 17
0
        long TestLocalHeader(ZipEntry entry, HeaderTest tests)
        {
            lock(baseStream_)
            {
                bool testHeader = (tests & HeaderTest.Header) != 0;
                bool testData = (tests & HeaderTest.Extract) != 0;

                baseStream_.Seek(offsetOfFirstEntry + entry.Offset, SeekOrigin.Begin);
                if ((int)ReadLEUint() != ZipConstants.LocalHeaderSignature) {
                    throw new ZipException(string.Format("Wrong local header signature @{0:X}", offsetOfFirstEntry + entry.Offset));
                }

                short extractVersion = ( short )ReadLEUshort();
                short localFlags = ( short )ReadLEUshort();
                short compressionMethod = ( short )ReadLEUshort();
                short fileTime = ( short )ReadLEUshort();
                short fileDate = ( short )ReadLEUshort();
                uint crcValue = ReadLEUint();
                long compressedSize = ReadLEUint();
                long size = ReadLEUint();
                int storedNameLength = ReadLEUshort();
                int extraDataLength = ReadLEUshort();

                byte[] nameData = new byte[storedNameLength];
                StreamUtils.ReadFully(baseStream_, nameData);

                byte[] extraData = new byte[extraDataLength];
                StreamUtils.ReadFully(baseStream_, extraData);

                ZipExtraData localExtraData = new ZipExtraData(extraData);

                if (localExtraData.Find(1))
                {
                    if (extractVersion < ZipConstants.VersionZip64)
                    {
                        throw new ZipException(
                            string.Format("Extra data contains Zip64 information but version {0}.{1} is not high enough",
                            extractVersion / 10, extractVersion % 10));
                    }

                    if (((uint)size != uint.MaxValue) && ((uint)compressedSize != uint.MaxValue))
                    {
                        throw new ZipException("Entry sizes not correct for Zip64");
                    }

                    size = localExtraData.ReadLong();
                    compressedSize = localExtraData.ReadLong();

                    if ((localFlags & (int)GeneralBitFlags.Descriptor) != 0)
                    {
                        if ( (size != -1) && (size != entry.Size)) {
                            throw new ZipException("Size invalid for descriptor");
                        }

                        if ((compressedSize != -1) && (compressedSize != entry.CompressedSize)) {
                            throw new ZipException("Compressed size invalid for descriptor");
                        }
                    }
                }
                else
                {
                    if ((extractVersion >= ZipConstants.VersionZip64) &&
                        (((uint)size == uint.MaxValue) || ((uint)compressedSize == uint.MaxValue)))
                    {
                        throw new ZipException("Required Zip64 extended information missing");
                    }
                }

                if ( testData ) {
                    if ( entry.IsFile ) {
                        if ( !entry.IsCompressionMethodSupported() ) {
                            throw new ZipException("Compression method not supported");
                        }

                        if ( (extractVersion > ZipConstants.VersionMadeBy)
                            || ((extractVersion > 20) && (extractVersion < ZipConstants.VersionZip64)) ) {
                            throw new ZipException(string.Format("Version required to extract this entry not supported ({0})", extractVersion));
                        }

                        if ( (localFlags & ( int )(GeneralBitFlags.Patched | GeneralBitFlags.StrongEncryption | GeneralBitFlags.EnhancedCompress | GeneralBitFlags.HeaderMasked)) != 0 ) {
                            throw new ZipException("The library does not support the zip version required to extract this entry");
                        }
                    }
                }

                if (testHeader)
                {
                    if ((extractVersion <= 63) &&
                        (extractVersion != 10) &&
                        (extractVersion != 11) &&
                        (extractVersion != 20) &&
                        (extractVersion != 21) &&
                        (extractVersion != 25) &&
                        (extractVersion != 27) &&
                        (extractVersion != 45) &&
                        (extractVersion != 46) &&
                        (extractVersion != 50) &&
                        (extractVersion != 51) &&
                        (extractVersion != 52) &&
                        (extractVersion != 61) &&
                        (extractVersion != 62) &&
                        (extractVersion != 63)
                        )
                    {
                        throw new ZipException(string.Format("Version required to extract this entry is invalid ({0})", extractVersion));
                    }

                    if ((localFlags & (int)(GeneralBitFlags.ReservedPKware4 | GeneralBitFlags.ReservedPkware14 | GeneralBitFlags.ReservedPkware15)) != 0)
                    {
                        throw new ZipException("Reserved bit flags cannot be set.");
                    }

                    if (((localFlags & (int)GeneralBitFlags.Encrypted) != 0) && (extractVersion < 20))
                    {
                        throw new ZipException(string.Format("Version required to extract this entry is too low for encryption ({0})", extractVersion));
                    }

                    if ((localFlags & (int)GeneralBitFlags.StrongEncryption) != 0)
                    {
                        if ((localFlags & (int)GeneralBitFlags.Encrypted) == 0)
                        {
                            throw new ZipException("Strong encryption flag set but encryption flag is not set");
                        }

                        if (extractVersion < 50)
                        {
                            throw new ZipException(string.Format("Version required to extract this entry is too low for encryption ({0})", extractVersion));
                        }
                    }

                    if (((localFlags & (int)GeneralBitFlags.Patched) != 0) && (extractVersion < 27))
                    {
                        throw new ZipException(string.Format("Patched data requires higher version than ({0})", extractVersion));
                    }

                    if (localFlags != entry.Flags)
                    {
                        throw new ZipException("Central header/local header flags mismatch");
                    }

                    if (entry.CompressionMethod != (CompressionMethod)compressionMethod)
                    {
                        throw new ZipException("Central header/local header compression method mismatch");
                    }

                    if (entry.Version != extractVersion)
                    {
                        throw new ZipException("Extract version mismatch");
                    }

                    if ((localFlags & (int)GeneralBitFlags.StrongEncryption) != 0)
                    {
                        if (extractVersion < 62)
                        {
                            throw new ZipException("Strong encryption flag set but version not high enough");
                        }
                    }

                    if ((localFlags & (int)GeneralBitFlags.HeaderMasked) != 0)
                    {
                        if ((fileTime != 0) || (fileDate != 0))
                        {
                            throw new ZipException("Header masked set but date/time values non-zero");
                        }
                    }

                    if ((localFlags & (int)GeneralBitFlags.Descriptor) == 0)
                    {
                        if (crcValue != (uint)entry.Crc)
                        {
                            throw new ZipException("Central header/local header crc mismatch");
                        }
                    }

                    if ((size == 0) && (compressedSize == 0))
                    {
                        if (crcValue != 0)
                        {
                            throw new ZipException("Invalid CRC for empty entry");
                        }
                    }

                    if (entry.Name.Length > storedNameLength)
                    {
                        throw new ZipException("File name length mismatch");
                    }

                    string localName = ZipConstants.ConvertToStringExt(localFlags, nameData);

                    if (localName != entry.Name)
                    {
                        throw new ZipException("Central header and local header file name mismatch");
                    }

                    if (entry.IsDirectory)
                    {
                        if (size > 0)
                        {
                            throw new ZipException("Directory cannot have size");
                        }

                        if (entry.IsCrypted)
                        {
                            if (compressedSize > ZipConstants.CryptoHeaderSize + 2)
                            {
                                throw new ZipException("Directory compressed size invalid");
                            }
                        }
                        else if (compressedSize > 2)
                        {
                            throw new ZipException("Directory compressed size invalid");
                        }
                    }

                    if (!ZipNameTransform.IsValidName(localName, true))
                    {
                        throw new ZipException("Name is invalid");
                    }
                }

                if (((localFlags & (int)GeneralBitFlags.Descriptor) == 0) ||
                    ((size > 0) || (compressedSize > 0))) {

                    if (size != entry.Size) {
                        throw new ZipException(
                            string.Format("Size mismatch between central header({0}) and local header({1})",
                                entry.Size, size));
                    }

                    if (compressedSize != entry.CompressedSize) {
                        throw new ZipException(
                            string.Format("Compressed size mismatch between central header({0}) and local header({1})",
                            entry.CompressedSize, compressedSize));
                    }
                }

                int extraLength = storedNameLength + extraDataLength;
                return offsetOfFirstEntry + entry.Offset + ZipConstants.LocalHeaderBaseSize + extraLength;
            }
        }
Exemplo n.º 18
0
        public ZipEntry GetNextEntry()
        {
            if (crc == null) {
                throw new InvalidOperationException("Closed.");
            }

            if (entry != null) {
                CloseEntry();
            }

            int header = inputBuffer.ReadLeInt();

            if (header == ZipConstants.CentralHeaderSignature ||
                header == ZipConstants.EndOfCentralDirectorySignature ||
                header == ZipConstants.CentralHeaderDigitalSignature ||
                header == ZipConstants.ArchiveExtraDataSignature ||
                header == ZipConstants.Zip64CentralFileHeaderSignature) {

                Close();
                return null;
            }

            if ( (header == ZipConstants.SpanningTempSignature) || (header == ZipConstants.SpanningSignature) ) {
                header = inputBuffer.ReadLeInt();
            }

            if (header != ZipConstants.LocalHeaderSignature) {
                throw new ZipException("Wrong Local header signature: 0x" + String.Format("{0:X}", header));
            }

            short versionRequiredToExtract = (short)inputBuffer.ReadLeShort();

            flags          = inputBuffer.ReadLeShort();
            method         = inputBuffer.ReadLeShort();
            uint dostime   = (uint)inputBuffer.ReadLeInt();
            int crc2       = inputBuffer.ReadLeInt();
            csize          = inputBuffer.ReadLeInt();
            size           = inputBuffer.ReadLeInt();
            int nameLen    = inputBuffer.ReadLeShort();
            int extraLen   = inputBuffer.ReadLeShort();

            bool isCrypted = (flags & 1) == 1;

            byte[] buffer = new byte[nameLen];
            inputBuffer.ReadRawBuffer(buffer);

            string name = ZipConstants.ConvertToStringExt(flags, buffer);

            entry = new ZipEntry(name, versionRequiredToExtract);
            entry.Flags = flags;

            entry.CompressionMethod = (CompressionMethod)method;

            if ((flags & 8) == 0) {
                entry.Crc  = crc2 & 0xFFFFFFFFL;
                entry.Size = size & 0xFFFFFFFFL;
                entry.CompressedSize = csize & 0xFFFFFFFFL;

                entry.CryptoCheckValue = (byte)((crc2 >> 24) & 0xff);

            } else {

                if (crc2 != 0) {
                    entry.Crc = crc2 & 0xFFFFFFFFL;
                }

                if (size != 0) {
                    entry.Size = size & 0xFFFFFFFFL;
                }

                if (csize != 0) {
                    entry.CompressedSize = csize & 0xFFFFFFFFL;
                }

                entry.CryptoCheckValue = (byte)((dostime >> 8) & 0xff);
            }

            entry.DosTime = dostime;

            if (extraLen > 0) {
                byte[] extra = new byte[extraLen];
                inputBuffer.ReadRawBuffer(extra);
                entry.ExtraData = extra;
            }

            entry.ProcessExtraData(true);
            if ( entry.CompressedSize >= 0 ) {
                csize = entry.CompressedSize;
            }

            if ( entry.Size >= 0 ) {
                size = entry.Size;
            }

            if (method == (int)CompressionMethod.Stored && (!isCrypted && csize != size || (isCrypted && csize - ZipConstants.CryptoHeaderSize != size))) {
                throw new ZipException("Stored, but compressed != uncompressed");
            }

            if (entry.IsCompressionMethodSupported()) {
                internalReader = new ReadDataHandler(InitialRead);
            } else {
                internalReader = new ReadDataHandler(ReadingNotSupported);
            }

            return entry;
        }
Exemplo n.º 19
0
        void ExtractEntry(ZipEntry entry)
        {
            bool   doExtraction = entry.IsCompressionMethodSupported();
            string targetName   = entry.Name;

            if (doExtraction)
            {
                if (entry.IsFile)
                {
                    targetName = extractNameTransform_.TransformFile(targetName);
                }
                else if (entry.IsDirectory)
                {
                    targetName = extractNameTransform_.TransformDirectory(targetName);
                }

                doExtraction = !string.IsNullOrEmpty(targetName);
            }

            // TODO: Fire delegate/throw exception were compression method not supported, or name is invalid?

            string dirName = null;

            if (doExtraction)
            {
                if (entry.IsDirectory)
                {
                    dirName = targetName;
                }
                else
                {
                    dirName = Path.GetDirectoryName(Path.GetFullPath(targetName));
                }
            }

            if (doExtraction && !Directory.Exists(dirName))
            {
                if (!entry.IsDirectory || CreateEmptyDirectories)
                {
                    try {
                        Directory.CreateDirectory(dirName);
                    }
                    catch (Exception ex) {
                        doExtraction = false;
                        if (events_ != null)
                        {
                            if (entry.IsDirectory)
                            {
                                continueRunning_ = events_.OnDirectoryFailure(targetName, ex);
                            }
                            else
                            {
                                continueRunning_ = events_.OnFileFailure(targetName, ex);
                            }
                        }
                        else
                        {
                            continueRunning_ = false;
                            throw;
                        }
                    }
                }
            }

            if (doExtraction && entry.IsFile)
            {
                ExtractFileEntry(entry, targetName);
            }
        }
Exemplo n.º 20
0
		void ExtractFileEntry(ZipEntry entry, string targetName)
		{
			bool proceed = true;
			if ( overwrite_ != Overwrite.Always ) {
#if !PCL
				if ( File.Exists(targetName) ) {
#else
                if (VFS.Current.FileExists(targetName))
                {
#endif
					if ( (overwrite_ == Overwrite.Prompt) && (confirmDelegate_ != null) ) {
						proceed = confirmDelegate_(targetName);
					}
					else {
						proceed = false;
					}
				}
			}
			
			if ( proceed ) {
				if ( events_ != null ) {
					continueRunning_ = events_.OnProcessFile(entry.Name);
				}
			
				if ( continueRunning_ ) {
					try
                    {
#if !PCL
						using ( FileStream outputStream = File.Create(targetName) ) {
#else
                        using (Stream outputStream = VFS.Current.CreateFile(targetName))
                        {
#endif
							if ( buffer_ == null ) {
								buffer_ = new byte[4096];
							}
							if ((events_ != null) && (events_.Progress != null))
							{
								StreamUtils.Copy(zipFile_.GetInputStream(entry), outputStream, buffer_,
									events_.Progress, events_.ProgressInterval, this, entry.Name, entry.Size);
							}
							else
							{
								StreamUtils.Copy(zipFile_.GetInputStream(entry), outputStream, buffer_);
							}
							
							if (events_ != null) {
								continueRunning_ = events_.OnCompletedFile(entry.Name);
							}
						}

#if !NETCF_1_0 && !NETCF_2_0
						if ( restoreDateTimeOnExtract_ )
                        {
#if !PCL
							File.SetLastWriteTime(targetName, entry.DateTime);
#else
                            VFS.Current.SetLastWriteTime(targetName, entry.DateTime);
#endif
						}
						
						if ( RestoreAttributesOnExtract && entry.IsDOSEntry && (entry.ExternalFileAttributes != -1)) {
							FileAttributes fileAttributes = (FileAttributes) entry.ExternalFileAttributes;
							// TODO: FastZip - Setting of other file attributes on extraction is a little trickier.
							fileAttributes &= (FileAttributes.Archive | FileAttributes.Normal | FileAttributes.ReadOnly | FileAttributes.Hidden);
#if !PCL
							File.SetAttributes(targetName, fileAttributes);
#else
                            VFS.Current.SetAttributes(targetName, fileAttributes);
#endif
						}
#endif						
					}
					catch(Exception ex) {
						if ( events_ != null ) {
							continueRunning_ = events_.OnFileFailure(targetName, ex);
						}
						else {
                            continueRunning_ = false;
                            throw;
						}
					}
				}
			}
		}

		void ExtractEntry(ZipEntry entry)
		{
			bool doExtraction = entry.IsCompressionMethodSupported();
			string targetName = entry.Name;
			
			if ( doExtraction ) {
				if ( entry.IsFile ) {
					targetName = extractNameTransform_.TransformFile(targetName);
				}
				else if ( entry.IsDirectory ) {
					targetName = extractNameTransform_.TransformDirectory(targetName);
				}
				
				doExtraction = !((targetName == null) || (targetName.Length == 0));
			}
			
			// TODO: Fire delegate/throw exception were compression method not supported, or name is invalid?

			string dirName = null;
			
			if ( doExtraction ) {
					if ( entry.IsDirectory ) {
						dirName = targetName;
					}
					else
                    {
#if !PCL
						dirName = Path.GetDirectoryName(Path.GetFullPath(targetName));
#else
                        dirName = Path.GetDirectoryName(VFS.Current.GetFullPath(targetName));
#endif
					}
            }

#if !PCL
			if ( doExtraction && !Directory.Exists(dirName) ) {
#else
            if (doExtraction && !VFS.Current.DirectoryExists(dirName))
            {
#endif
				if ( !entry.IsDirectory || CreateEmptyDirectories ) {
					try
                    {
#if !PCL
						Directory.CreateDirectory(dirName);
#else
                        VFS.Current.CreateDirectory(dirName);
#endif
					}
					catch (Exception ex) {
						doExtraction = false;
						if ( events_ != null ) {
							if ( entry.IsDirectory ) {
								continueRunning_ = events_.OnDirectoryFailure(targetName, ex);
							}
							else {
								continueRunning_ = events_.OnFileFailure(targetName, ex);
							}
						}
						else {
							continueRunning_ = false;
                            throw;
						}
					}
				}
			}
			
			if ( doExtraction && entry.IsFile ) {
				ExtractFileEntry(entry, targetName);
			}
		}
#if !PCL
		static int MakeExternalAttributes(FileInfo info)
		{
			return (int)info.Attributes;
		}
#else
        static int MakeExternalAttributes(IFileInfo info)
        {
            return (int)info.Attributes;
        }
Exemplo n.º 21
0
 public bool IsCompressionMethodSupported()
 {
     return(ZipEntry.IsCompressionMethodSupported(this.CompressionMethod));
 }
Exemplo n.º 22
0
		void ExtractEntry(ZipEntry entry)
		{
			bool doExtraction = entry.IsCompressionMethodSupported();
			string targetName = entry.Name;
		    using (var store = IsolatedStorageFile.GetUserStoreForApplication())
		    {
		        if ( doExtraction ) {
		            if ( entry.IsFile ) {
		                targetName = extractNameTransform_.TransformFile(targetName);
		            }
		            else if ( entry.IsDirectory ) {
		                targetName = extractNameTransform_.TransformDirectory(targetName);
		            }
				
		            doExtraction = !string.IsNullOrEmpty(targetName);
		        }
			
		        // TODO: Fire delegate/throw exception were compression method not supported, or name is invalid?

		        string dirName = null;
			
		        if ( doExtraction ) {
		            if ( entry.IsDirectory ) {
		                dirName = targetName;
		            }
		            else {
		                dirName = Path.GetDirectoryName(targetName);
		            }
		        }

		        if (doExtraction && !store.DirectoryExists(dirName))
		        {
		            if ( !entry.IsDirectory || CreateEmptyDirectories ) {
		                try {
		                    store.CreateDirectory(dirName);
		                }
		                catch (Exception ex) {
		                    doExtraction = false;
		                    if ( events_ != null ) {
		                        if ( entry.IsDirectory ) {
		                            continueRunning_ = events_.OnDirectoryFailure(targetName, ex);
		                        }
		                        else {
		                            continueRunning_ = events_.OnFileFailure(targetName, ex);
		                        }
		                    }
		                    else {
		                        continueRunning_ = false;
		                        throw;
		                    }
		                }
		            }
		        }
		    }

		    if ( doExtraction && entry.IsFile ) {
				ExtractFileEntry(entry, targetName);
			}
		}
Exemplo n.º 23
0
        void ExtractFileEntry(ZipEntry entry, string targetName)
        {
            bool proceed = true;

            if (overwrite_ != Overwrite.Always)
            {
#if !PCL
                if (File.Exists(targetName))
                {
#else
                if (VFS.Current.FileExists(targetName))
                {
#endif
                    if ((overwrite_ == Overwrite.Prompt) && (confirmDelegate_ != null))
                    {
                        proceed = confirmDelegate_(targetName);
                    }
                    else
                    {
                        proceed = false;
                    }
                }
            }

            if (proceed)
            {
                if (events_ != null)
                {
                    continueRunning_ = events_.OnProcessFile(entry.Name);
                }

                if (continueRunning_)
                {
                    try
                    {
#if !PCL
                        using (FileStream outputStream = File.Create(targetName)) {
#else
                        using (Stream outputStream = VFS.Current.CreateFile(targetName))
                        {
#endif
                            if (buffer_ == null)
                            {
                                buffer_ = new byte[4096];
                            }
                            if ((events_ != null) && (events_.Progress != null))
                            {
                                StreamUtils.Copy(zipFile_.GetInputStream(entry), outputStream, buffer_,
                                                 events_.Progress, events_.ProgressInterval, this, entry.Name, entry.Size);
                            }
                            else
                            {
                                StreamUtils.Copy(zipFile_.GetInputStream(entry), outputStream, buffer_);
                            }

                            if (events_ != null)
                            {
                                continueRunning_ = events_.OnCompletedFile(entry.Name);
                            }
                        }

#if !NETCF_1_0 && !NETCF_2_0
                        if (restoreDateTimeOnExtract_)
                        {
#if !PCL
                            File.SetLastWriteTime(targetName, entry.DateTime);
#else
                            VFS.Current.SetLastWriteTime(targetName, entry.DateTime);
#endif
                        }

                        if (RestoreAttributesOnExtract && entry.IsDOSEntry && (entry.ExternalFileAttributes != -1))
                        {
                            FileAttributes fileAttributes = (FileAttributes)entry.ExternalFileAttributes;
                            // TODO: FastZip - Setting of other file attributes on extraction is a little trickier.
                            fileAttributes &= (FileAttributes.Archive | FileAttributes.Normal | FileAttributes.ReadOnly | FileAttributes.Hidden);
#if !PCL
                            File.SetAttributes(targetName, fileAttributes);
#else
                            VFS.Current.SetAttributes(targetName, fileAttributes);
#endif
                        }
#endif
                    }
                    catch (Exception ex) {
                        if (events_ != null)
                        {
                            continueRunning_ = events_.OnFileFailure(targetName, ex);
                        }
                        else
                        {
                            continueRunning_ = false;
                            throw;
                        }
                    }
                }
            }
        }

        void ExtractEntry(ZipEntry entry)
        {
            bool   doExtraction = entry.IsCompressionMethodSupported();
            string targetName   = entry.Name;

            if (doExtraction)
            {
                if (entry.IsFile)
                {
                    targetName = extractNameTransform_.TransformFile(targetName);
                }
                else if (entry.IsDirectory)
                {
                    targetName = extractNameTransform_.TransformDirectory(targetName);
                }

                doExtraction = !((targetName == null) || (targetName.Length == 0));
            }

            // TODO: Fire delegate/throw exception were compression method not supported, or name is invalid?

            string dirName = null;

            if (doExtraction)
            {
                if (entry.IsDirectory)
                {
                    dirName = targetName;
                }
                else
                {
#if !PCL
                    dirName = Path.GetDirectoryName(Path.GetFullPath(targetName));
#else
                    dirName = Path.GetDirectoryName(VFS.Current.GetFullPath(targetName));
#endif
                }
            }

#if !PCL
            if (doExtraction && !Directory.Exists(dirName))
            {
#else
            if (doExtraction && !VFS.Current.DirectoryExists(dirName))
            {
#endif
                if (!entry.IsDirectory || CreateEmptyDirectories)
                {
                    try
                    {
#if !PCL
                        Directory.CreateDirectory(dirName);
#else
                        VFS.Current.CreateDirectory(dirName);
#endif
                    }
                    catch (Exception ex) {
                        doExtraction = false;
                        if (events_ != null)
                        {
                            if (entry.IsDirectory)
                            {
                                continueRunning_ = events_.OnDirectoryFailure(targetName, ex);
                            }
                            else
                            {
                                continueRunning_ = events_.OnFileFailure(targetName, ex);
                            }
                        }
                        else
                        {
                            continueRunning_ = false;
                            throw;
                        }
                    }
                }
            }

            if (doExtraction && entry.IsFile)
            {
                ExtractFileEntry(entry, targetName);
            }
        }
Exemplo n.º 24
0
		void ExtractEntry(ZipEntry entry)
		{
			bool doExtraction = entry.IsCompressionMethodSupported();
			string targetName = entry.Name;
			
			if ( doExtraction ) {
				if ( entry.IsFile ) {
					targetName = extractNameTransform_.TransformFile(targetName);
				}
				else if ( entry.IsDirectory ) {
					targetName = extractNameTransform_.TransformDirectory(targetName);
				}
				
				doExtraction = !((targetName == null) || (targetName.Length == 0));
			}
			
			// TODO: Fire delegate/throw exception were compression method not supported, or name is invalid?

			string dirName = null;
			
			if ( doExtraction ) {
					if ( entry.IsDirectory ) {
						dirName = targetName;
					}
					else {
						dirName = Path.GetDirectoryName(Path.GetFullPath(targetName));
					}
			}
			
			if ( doExtraction && !Directory.Exists(dirName) ) {
				if ( !entry.IsDirectory || CreateEmptyDirectories ) {
					try {
						Directory.CreateDirectory(dirName);
					}
					catch (Exception ex) {
						doExtraction = false;
						if ( events_ != null ) {
							if ( entry.IsDirectory ) {
								continueRunning_ = events_.OnDirectoryFailure(targetName, ex);
							}
							else {
								continueRunning_ = events_.OnFileFailure(targetName, ex);
							}
						}
						else {
							continueRunning_ = false;
                            throw;
						}
					}
				}
			}
			
			if ( doExtraction && entry.IsFile ) {
				ExtractFileEntry(entry, targetName);
			}
		}
Exemplo n.º 25
0
        void ExtractEntry(ZipEntry entry)
        {
            bool doExtraction = NameIsValid(entry.Name) && entry.IsCompressionMethodSupported();

            // TODO: Fire delegate were compression method not supported.

            string dirName = null;
            string targetName = null;

            if ( doExtraction ) {
                string entryFileName;
                if (Path.IsPathRooted(entry.Name)) {
                    string workName = Path.GetPathRoot(entry.Name);
                    workName = entry.Name.Substring(workName.Length);
                    entryFileName = Path.Combine(Path.GetDirectoryName(workName), Path.GetFileName(entry.Name));
                }
                else {
                    entryFileName = entry.Name;
                }

                targetName = Path.Combine(targetDirectory_, entryFileName);
                dirName = Path.GetDirectoryName(Path.GetFullPath(targetName));

                doExtraction = (entryFileName.Length > 0);
            }

            if ( doExtraction && !Directory.Exists(dirName) ) {
                if ( !entry.IsDirectory || CreateEmptyDirectories ) {
                    try {
                        Directory.CreateDirectory(dirName);
                    }
                    catch {
                        doExtraction = false;
                    }
                }
            }

            if ( doExtraction && entry.IsFile ) {
                ExtractFileEntry(entry, targetName);
            }
        }
Exemplo n.º 26
0
		/// <summary>
		/// Advances to the next entry in the archive
		/// </summary>
		/// <returns>
		/// The next <see cref="ZipEntry">entry</see> in the archive or null if there are no more entries.
		/// </returns>
		/// <remarks>
		/// If the previous entry is still open <see cref="CloseEntry">CloseEntry</see> is called.
		/// </remarks>
		/// <exception cref="InvalidOperationException">
		/// Input stream is closed
		/// </exception>
		/// <exception cref="ZipException">
		/// Password is not set, password is invalid, compression method is invalid,
		/// version required to extract is not supported
		/// </exception>
		public ZipEntry GetNextEntry()
		{
			if (crc == null) {
				throw new InvalidOperationException("Closed.");
			}
			
			if (entry != null) {
				CloseEntry();
			}
			
			int header = inputBuffer.ReadLeInt();
			
			if (header == ZipConstants.CentralHeaderSignature ||
				header == ZipConstants.EndOfCentralDirectorySignature ||
				header == ZipConstants.CentralHeaderDigitalSignature ||
				header == ZipConstants.ArchiveExtraDataSignature ||
				header == ZipConstants.Zip64CentralFileHeaderSignature) {
				// No more individual entries exist
				Close();
				return null;
			}
			
			// -jr- 07-Dec-2003 Ignore spanning temporary signatures if found
			// Spanning signature is same as descriptor signature and is untested as yet.
			if ( (header == ZipConstants.SpanningTempSignature) || (header == ZipConstants.SpanningSignature) ) {
				header = inputBuffer.ReadLeInt();
			}
			
			if (header != ZipConstants.LocalHeaderSignature) {
				throw new ZipException("Wrong Local header signature: 0x" + String.Format("{0:X}", header));
			}
			
			short versionRequiredToExtract = (short)inputBuffer.ReadLeShort();
			
			flags          = inputBuffer.ReadLeShort();
			method         = inputBuffer.ReadLeShort();
			uint dostime   = (uint)inputBuffer.ReadLeInt();
			int crc2       = inputBuffer.ReadLeInt();
			csize          = inputBuffer.ReadLeInt();
			size           = inputBuffer.ReadLeInt();
			int nameLen    = inputBuffer.ReadLeShort();
			int extraLen   = inputBuffer.ReadLeShort();
			
			bool isCrypted = (flags & 1) == 1;
			
			byte[] buffer = new byte[nameLen];
			inputBuffer.ReadRawBuffer(buffer);
			
			string name = ZipConstants.ConvertToStringExt(flags, buffer);
			
			entry = new ZipEntry(name, versionRequiredToExtract);
			entry.Flags = flags;
			
			entry.CompressionMethod = (CompressionMethod)method;
			
			if ((flags & 8) == 0) {
				entry.Crc  = crc2 & 0xFFFFFFFFL;
				entry.Size = size & 0xFFFFFFFFL;
				entry.CompressedSize = csize & 0xFFFFFFFFL;

				entry.CryptoCheckValue = (byte)((crc2 >> 24) & 0xff);

			} else {
				
				// This allows for GNU, WinZip and possibly other archives, the PKZIP spec
				// says these values are zero under these circumstances.
				if (crc2 != 0) {
					entry.Crc = crc2 & 0xFFFFFFFFL;
				}
				
				if (size != 0) {
					entry.Size = size & 0xFFFFFFFFL;
				}

				if (csize != 0) {
					entry.CompressedSize = csize & 0xFFFFFFFFL;
				}

				entry.CryptoCheckValue = (byte)((dostime >> 8) & 0xff);
			}
			
			entry.DosTime = dostime;

			// If local header requires Zip64 is true then the extended header should contain
			// both values.

			// Handle extra data if present.  This can set/alter some fields of the entry.
			if (extraLen > 0) {
				byte[] extra = new byte[extraLen];
				inputBuffer.ReadRawBuffer(extra);
				entry.ExtraData = extra;
			}

			entry.ProcessExtraData(true);
			if ( entry.CompressedSize >= 0 ) {
				csize = entry.CompressedSize;
			}

			if ( entry.Size >= 0 ) {
				size = entry.Size;
			}
			
			if (method == (int)CompressionMethod.Stored && (!isCrypted && csize != size || (isCrypted && csize - ZipConstants.CryptoHeaderSize != size))) {
				throw new ZipException("Stored, but compressed != uncompressed");
			}

			// Determine how to handle reading of data if this is attempted.
			if (entry.IsCompressionMethodSupported()) {
				internalReader = new ReadDataHandler(InitialRead);
			} else {
				internalReader = new ReadDataHandler(ReadingNotSupported);
			}
			
			return entry;
		}
Exemplo n.º 27
0
        void ExtractEntry(ZipEntry entry)
        {
            bool   doExtraction = entry.IsCompressionMethodSupported();
            string targetName   = entry.Name;

            if (doExtraction)
            {
                if (entry.IsFile)
                {
                    targetName = extractNameTransform_.TransformFile(targetName);
                }
                else if (entry.IsDirectory)
                {
                    targetName = extractNameTransform_.TransformDirectory(targetName);
                }

                doExtraction = !((targetName == null) || (targetName.Length == 0));
            }


            string dirName = null;

            if (doExtraction)
            {
                if (entry.IsDirectory)
                {
                    dirName = targetName;
                }
                else
                {
                    dirName = Path.GetDirectoryName(Path.GetFullPath(targetName));
                }
            }

            if (doExtraction && !Directory.Exists(dirName))
            {
                if (!entry.IsDirectory || CreateEmptyDirectories)
                {
                    try {
                        Directory.CreateDirectory(dirName);
                    }
                    catch (Exception ex) {
                        doExtraction = false;
                        if (events_ != null)
                        {
                            if (entry.IsDirectory)
                            {
                                continueRunning_ = events_.OnDirectoryFailure(targetName, ex);
                            }
                            else
                            {
                                continueRunning_ = events_.OnFileFailure(targetName, ex);
                            }
                        }
                        else
                        {
                            continueRunning_ = false;
                            throw;
                        }
                    }
                }
            }

            if (doExtraction && entry.IsFile)
            {
                ExtractFileEntry(entry, targetName);
            }
        }
Exemplo n.º 28
0
        void ExtractEntry(ZipEntry entry)
        {
            bool doExtraction = false;

            string nameText = entry.Name;

            if ( entry.IsFile ) {
                //-TODO Translate invalid names allowing extraction still.
                doExtraction = NameIsValid(nameText) && entry.IsCompressionMethodSupported();
            }
            else if ( entry.IsDirectory ) {
                doExtraction = NameIsValid(nameText);
            }

            //-TODO Fire delegate were compression method not supported, or name is invalid?

            string dirName = null;
            string targetName = null;

            if ( doExtraction ) {
                // Handle invalid entry names by chopping of path root.
                if (Path.IsPathRooted(nameText)) {
                    string workName = Path.GetPathRoot(nameText);
                    nameText = nameText.Substring(workName.Length);
                }

                if ( nameText.Length > 0 ) {
                    targetName = Path.Combine(targetDirectory_, nameText);
                    if ( entry.IsDirectory ) {
                        dirName = targetName;
                    }
                    else {
                        dirName = Path.GetDirectoryName(Path.GetFullPath(targetName));
                    }
                }
                else {
                    doExtraction = false;
                }
            }

            if ( doExtraction && !Directory.Exists(dirName) ) {
                if ( !entry.IsDirectory || CreateEmptyDirectories ) {
                    try {
                        Directory.CreateDirectory(dirName);
                    }
                    catch (Exception ex) {
                        doExtraction = false;
                        if ( events_ != null ) {
                            if ( entry.IsDirectory ) {
                                continueRunning_ = events_.OnDirectoryFailure(targetName, ex);
                            }
                            else {
                                continueRunning_ = events_.OnFileFailure(targetName, ex);
                            }
                        }
                        else {
                            continueRunning_ = false;
                        }
                    }
                }
            }

            if ( doExtraction && entry.IsFile ) {
                ExtractFileEntry(entry, targetName);
            }
        }
Exemplo n.º 29
0
        private void ExtractEntry(ZipEntry entry)
        {
            bool   doExtraction = entry.IsCompressionMethodSupported();
            string targetName   = entry.Name;

            if (doExtraction)
            {
                if (entry.IsFile)
                {
                    targetName = extractNameTransform_.TransformFile(targetName);
                }
                else if (entry.IsDirectory)
                {
                    targetName = extractNameTransform_.TransformDirectory(targetName);
                }

                doExtraction = !(string.IsNullOrEmpty(targetName));
            }

            // TODO: Fire delegate/throw exception were compression method not supported, or name is invalid?

            string dirName = string.Empty;

            if (doExtraction)
            {
                if (entry.IsDirectory)
                {
                    dirName = targetName;
                }
                else
                {
                    dirName = Path.GetDirectoryName(Path.GetFullPath(targetName));
                }
            }

            if (doExtraction && !Directory.Exists(dirName))
            {
                if (!entry.IsDirectory || CreateEmptyDirectories)
                {
                    try
                    {
                        continueRunning_ = events_?.OnProcessDirectory(dirName, true) ?? true;
                        if (continueRunning_)
                        {
                            Directory.CreateDirectory(dirName);
                            if (entry.IsDirectory && restoreDateTimeOnExtract_)
                            {
                                switch (entryFactory_.Setting)
                                {
                                case TimeSetting.CreateTime:
                                    Directory.SetCreationTime(dirName, entry.DateTime);
                                    break;

                                case TimeSetting.CreateTimeUtc:
                                    Directory.SetCreationTimeUtc(dirName, entry.DateTime);
                                    break;

                                case TimeSetting.LastAccessTime:
                                    Directory.SetLastAccessTime(dirName, entry.DateTime);
                                    break;

                                case TimeSetting.LastAccessTimeUtc:
                                    Directory.SetLastAccessTimeUtc(dirName, entry.DateTime);
                                    break;

                                case TimeSetting.LastWriteTime:
                                    Directory.SetLastWriteTime(dirName, entry.DateTime);
                                    break;

                                case TimeSetting.LastWriteTimeUtc:
                                    Directory.SetLastWriteTimeUtc(dirName, entry.DateTime);
                                    break;

                                case TimeSetting.Fixed:
                                    Directory.SetLastWriteTime(dirName, entryFactory_.FixedDateTime);
                                    break;

                                default:
                                    throw new ZipException("Unhandled time setting in ExtractEntry");
                                }
                            }
                        }
                        else
                        {
                            doExtraction = false;
                        }
                    }
                    catch (Exception ex)
                    {
                        doExtraction = false;
                        if (events_ != null)
                        {
                            if (entry.IsDirectory)
                            {
                                continueRunning_ = events_.OnDirectoryFailure(targetName, ex);
                            }
                            else
                            {
                                continueRunning_ = events_.OnFileFailure(targetName, ex);
                            }
                        }
                        else
                        {
                            continueRunning_ = false;
                            throw;
                        }
                    }
                }
            }

            if (doExtraction && entry.IsFile)
            {
                ExtractFileEntry(entry, targetName);
            }
        }
Exemplo n.º 30
0
		/// <summary>
		/// Test a local header against that provided from the central directory
		/// </summary>
		/// <param name="entry">
		/// The entry to test against
		/// </param>
		/// <param name="tests">The type of <see cref="HeaderTest">tests</see> to carry out.</param>
		/// <returns>The offset of the entries data in the file</returns>
		long TestLocalHeader(ZipEntry entry, HeaderTest tests)
		{
			lock(baseStream_) 
			{
				bool testHeader = (tests & HeaderTest.Header) != 0;
				bool testData = (tests & HeaderTest.Extract) != 0;

				baseStream_.Seek(offsetOfFirstEntry + entry.Offset, SeekOrigin.Begin);
				if ((int)ReadLEUint() != ZipConstants.LocalHeaderSignature) {
					throw new ZipException(string.Format("Wrong local header signature @{0:X}", offsetOfFirstEntry + entry.Offset));
				}

				short extractVersion = ( short ) (ReadLEUshort() & 0x00ff);
				short localFlags = ( short )ReadLEUshort();
				short compressionMethod = ( short )ReadLEUshort();
				short fileTime = ( short )ReadLEUshort();
				short fileDate = ( short )ReadLEUshort();
				uint crcValue = ReadLEUint();
				long compressedSize = ReadLEUint();
				long size = ReadLEUint();
				int storedNameLength = ReadLEUshort();
				int extraDataLength = ReadLEUshort();

				byte[] nameData = new byte[storedNameLength];
				StreamUtils.ReadFully(baseStream_, nameData);

				byte[] extraData = new byte[extraDataLength];
				StreamUtils.ReadFully(baseStream_, extraData);

				ZipExtraData localExtraData = new ZipExtraData(extraData);

				// Extra data / zip64 checks
				if (localExtraData.Find(1))
				{
					// 2010-03-04 Forum 10512: removed checks for version >= ZipConstants.VersionZip64
					// and size or compressedSize = MaxValue, due to rogue creators.

					size = localExtraData.ReadLong();
					compressedSize = localExtraData.ReadLong();

                    if ((localFlags & (int)GeneralBitFlags.Descriptor) != 0)
                    {
                        // These may be valid if patched later
                        if ( (size != -1) && (size != entry.Size)) {
                            throw new ZipException("Size invalid for descriptor");
                        }

                        if ((compressedSize != -1) && (compressedSize != entry.CompressedSize)) {
                            throw new ZipException("Compressed size invalid for descriptor");
                        }
                    }
                }
				else
				{
					// No zip64 extra data but entry requires it.
					if ((extractVersion >= ZipConstants.VersionZip64) &&
						(((uint)size == uint.MaxValue) || ((uint)compressedSize == uint.MaxValue)))
					{
						throw new ZipException("Required Zip64 extended information missing");
					}
				}

				if ( testData ) {
					if ( entry.IsFile ) {
						if ( !entry.IsCompressionMethodSupported() ) {
							throw new ZipException("Compression method not supported");
						}

						if ( (extractVersion > ZipConstants.VersionMadeBy)
							|| ((extractVersion > 20) && (extractVersion < ZipConstants.VersionZip64)) ) {
							throw new ZipException(string.Format("Version required to extract this entry not supported ({0})", extractVersion));
						}

						if ( (localFlags & ( int )(GeneralBitFlags.Patched | GeneralBitFlags.StrongEncryption | GeneralBitFlags.EnhancedCompress | GeneralBitFlags.HeaderMasked)) != 0 ) {
							throw new ZipException("The library does not support the zip version required to extract this entry");
						}
					}
				}

                if (testHeader)
                {
                    if ((extractVersion <= 63) &&	// Ignore later versions as we dont know about them..
                        (extractVersion != 10) &&
                        (extractVersion != 11) &&
                        (extractVersion != 20) &&
                        (extractVersion != 21) &&
                        (extractVersion != 25) &&
                        (extractVersion != 27) &&
                        (extractVersion != 45) &&
                        (extractVersion != 46) &&
                        (extractVersion != 50) &&
                        (extractVersion != 51) &&
                        (extractVersion != 52) &&
                        (extractVersion != 61) &&
                        (extractVersion != 62) &&
                        (extractVersion != 63)
                        )
                    {
                        throw new ZipException(string.Format("Version required to extract this entry is invalid ({0})", extractVersion));
                    }

                    // Local entry flags dont have reserved bit set on.
                    if ((localFlags & (int)(GeneralBitFlags.ReservedPKware4 | GeneralBitFlags.ReservedPkware14 | GeneralBitFlags.ReservedPkware15)) != 0)
                    {
                        throw new ZipException("Reserved bit flags cannot be set.");
                    }

                    // Encryption requires extract version >= 20
                    if (((localFlags & (int)GeneralBitFlags.Encrypted) != 0) && (extractVersion < 20))
                    {
                        throw new ZipException(string.Format("Version required to extract this entry is too low for encryption ({0})", extractVersion));
                    }

                    // Strong encryption requires encryption flag to be set and extract version >= 50.
                    if ((localFlags & (int)GeneralBitFlags.StrongEncryption) != 0)
                    {
                        if ((localFlags & (int)GeneralBitFlags.Encrypted) == 0)
                        {
                            throw new ZipException("Strong encryption flag set but encryption flag is not set");
                        }

                        if (extractVersion < 50)
                        {
                            throw new ZipException(string.Format("Version required to extract this entry is too low for encryption ({0})", extractVersion));
                        }
                    }

                    // Patched entries require extract version >= 27
                    if (((localFlags & (int)GeneralBitFlags.Patched) != 0) && (extractVersion < 27))
                    {
                        throw new ZipException(string.Format("Patched data requires higher version than ({0})", extractVersion));
                    }

                    // Central header flags match local entry flags.
                    if (localFlags != entry.Flags)
                    {
                        throw new ZipException("Central header/local header flags mismatch");
                    }

                    // Central header compression method matches local entry
                    if (entry.CompressionMethod != (CompressionMethod)compressionMethod)
                    {
                        throw new ZipException("Central header/local header compression method mismatch");
                    }

                    if (entry.Version != extractVersion)
                    {
                        throw new ZipException("Extract version mismatch");
                    }

                    // Strong encryption and extract version match
                    if ((localFlags & (int)GeneralBitFlags.StrongEncryption) != 0)
                    {
                        if (extractVersion < 62)
                        {
                            throw new ZipException("Strong encryption flag set but version not high enough");
                        }
                    }

                    if ((localFlags & (int)GeneralBitFlags.HeaderMasked) != 0)
                    {
                        if ((fileTime != 0) || (fileDate != 0))
                        {
                            throw new ZipException("Header masked set but date/time values non-zero");
                        }
                    }

                    if ((localFlags & (int)GeneralBitFlags.Descriptor) == 0)
                    {
                        if (crcValue != (uint)entry.Crc)
                        {
                            throw new ZipException("Central header/local header crc mismatch");
                        }
                    }

                    // Crc valid for empty entry.
                    // This will also apply to streamed entries where size isnt known and the header cant be patched
                    if ((size == 0) && (compressedSize == 0))
                    {
                        if (crcValue != 0)
                        {
                            throw new ZipException("Invalid CRC for empty entry");
                        }
                    }

                    // TODO: make test more correct...  can't compare lengths as was done originally as this can fail for MBCS strings
                    // Assuming a code page at this point is not valid?  Best is to store the name length in the ZipEntry probably
                    if (entry.Name.Length > storedNameLength)
                    {
                        throw new ZipException("File name length mismatch");
                    }

                    // Name data has already been read convert it and compare.
                    string localName = ZipConstants.ConvertToStringExt(localFlags, nameData);

                    // Central directory and local entry name match
                    if (localName != entry.Name)
                    {
                        throw new ZipException("Central header and local header file name mismatch");
                    }

                    // Directories have zero actual size but can have compressed size
                    if (entry.IsDirectory)
                    {
                        if (size > 0)
                        {
                            throw new ZipException("Directory cannot have size");
                        }

                        // There may be other cases where the compressed size can be greater than this?
                        // If so until details are known we will be strict.
                        if (entry.IsCrypted)
                        {
                            if (compressedSize > ZipConstants.CryptoHeaderSize + 2)
                            {
                                throw new ZipException("Directory compressed size invalid");
                            }
                        }
                        else if (compressedSize > 2)
                        {
                            // When not compressed the directory size can validly be 2 bytes
                            // if the true size wasnt known when data was originally being written.
                            // NOTE: Versions of the library 0.85.4 and earlier always added 2 bytes
                            throw new ZipException("Directory compressed size invalid");
                        }
                    }

                    if (!ZipNameTransform.IsValidName(localName, true))
                    {
                        throw new ZipException("Name is invalid");
                    }
                }

				// Tests that apply to both data and header.

				// Size can be verified only if it is known in the local header.
				// it will always be known in the central header.
				if (((localFlags & (int)GeneralBitFlags.Descriptor) == 0) ||
					((size > 0 || compressedSize > 0) && entry.Size > 0)) {

					if ((size != 0)
						&& (size != entry.Size)) {
						throw new ZipException(
							string.Format("Size mismatch between central header({0}) and local header({1})",
								entry.Size, size));
					}

					if ((compressedSize != 0)
						&& (compressedSize != entry.CompressedSize && compressedSize != 0xFFFFFFFF && compressedSize != -1)) {
						throw new ZipException(
							string.Format("Compressed size mismatch between central header({0}) and local header({1})",
							entry.CompressedSize, compressedSize));
					}
				}

				int extraLength = storedNameLength + extraDataLength;
				return offsetOfFirstEntry + entry.Offset + ZipConstants.LocalHeaderBaseSize + extraLength;
			}
		}
Exemplo n.º 31
0
        public ZipEntry GetNextEntry()
        {
            //IL_000d: Unknown result type (might be due to invalid IL or missing references)
            if (crc == null)
            {
                throw new InvalidOperationException("Closed.");
            }
            if (entry != null)
            {
                CloseEntry();
            }
            int num = inputBuffer.ReadLeInt();

            switch (num)
            {
            case 33639248:
            case 84233040:
            case 101010256:
            case 101075792:
            case 117853008:
                ((Stream)this).Close();
                return(null);

            case 134695760:
            case 808471376:
                num = inputBuffer.ReadLeInt();
                break;
            }
            if (num != 67324752)
            {
                throw new ZipException("Wrong Local header signature: 0x" + $"{num:X}");
            }
            short versionRequiredToExtract = (short)inputBuffer.ReadLeShort();

            flags  = inputBuffer.ReadLeShort();
            method = inputBuffer.ReadLeShort();
            uint num2 = (uint)inputBuffer.ReadLeInt();
            int  num3 = inputBuffer.ReadLeInt();

            csize = inputBuffer.ReadLeInt();
            size  = inputBuffer.ReadLeInt();
            int  num4 = inputBuffer.ReadLeShort();
            int  num5 = inputBuffer.ReadLeShort();
            bool flag = (flags & 1) == 1;

            byte[] array = new byte[num4];
            inputBuffer.ReadRawBuffer(array);
            string name = ZipConstants.ConvertToStringExt(flags, array);

            entry                   = new ZipEntry(name, versionRequiredToExtract);
            entry.Flags             = flags;
            entry.CompressionMethod = (CompressionMethod)method;
            if ((flags & 8) == 0)
            {
                entry.Crc              = num3 & 0xFFFFFFFFu;
                entry.Size             = size & 0xFFFFFFFFu;
                entry.CompressedSize   = csize & 0xFFFFFFFFu;
                entry.CryptoCheckValue = (byte)((uint)(num3 >> 24) & 0xFFu);
            }
            else
            {
                if (num3 != 0)
                {
                    entry.Crc = num3 & 0xFFFFFFFFu;
                }
                if (size != 0)
                {
                    entry.Size = size & 0xFFFFFFFFu;
                }
                if (csize != 0)
                {
                    entry.CompressedSize = csize & 0xFFFFFFFFu;
                }
                entry.CryptoCheckValue = (byte)((num2 >> 8) & 0xFFu);
            }
            entry.DosTime = num2;
            if (num5 > 0)
            {
                byte[] array2 = new byte[num5];
                inputBuffer.ReadRawBuffer(array2);
                entry.ExtraData = array2;
            }
            entry.ProcessExtraData(localHeader: true);
            if (entry.CompressedSize >= 0)
            {
                csize = entry.CompressedSize;
            }
            if (entry.Size >= 0)
            {
                size = entry.Size;
            }
            if (method == 0 && ((!flag && csize != size) || (flag && csize - 12 != size)))
            {
                throw new ZipException("Stored, but compressed != uncompressed");
            }
            if (entry.IsCompressionMethodSupported())
            {
                internalReader = InitialRead;
            }
            else
            {
                internalReader = ReadingNotSupported;
            }
            return(entry);
        }