예제 #1
0
파일: ZipFile.cs 프로젝트: IntegralLee/fomm
    /// <summary>
    ///   Get a <see cref="Stream" /> providing data for an entry.
    /// </summary>
    /// <param name="entry">The entry to provide data for.</param>
    /// <param name="name">The file name for data if known.</param>
    /// <returns>Returns a stream providing data; or null if not available</returns>
    public Stream GetSource(ZipEntry entry, string name)
    {
      Stream result = null;

      if (name != null)
      {
        result = File.OpenRead(name);
      }

      return result;
    }
예제 #2
0
파일: ZipFile.cs 프로젝트: IntegralLee/fomm
 /// <summary>
 ///   Locate the data for a given entry.
 /// </summary>
 /// <returns>
 ///   The start offset of the data.
 /// </returns>
 /// <exception cref="System.IO.EndOfStreamException">
 ///   The stream ends prematurely
 /// </exception>
 /// <exception cref="ZipException">
 ///   The local header signature is invalid, the entry and central header file name lengths are different
 ///   or the local and entry compression methods dont match
 /// </exception>
 private long LocateEntry(ZipEntry entry)
 {
   return TestLocalHeader(entry, HeaderTest.Extract);
 }
예제 #3
0
파일: ZipFile.cs 프로젝트: IntegralLee/fomm
 public ZipEntryEnumerator(ZipEntry[] entries)
 {
   array = entries;
 }
예제 #4
0
    /// <summary>
    ///   Make a new <see cref="ZipEntry" /> from a name.
    /// </summary>
    /// <param name="fileName">The name of the file to create a new entry for.</param>
    /// <param name="useFileSystem">If true entry detail is retrieved from the file system if the file exists.</param>
    /// <returns>Returns a new <see cref="ZipEntry" /> based on the <paramref name="fileName" />.</returns>
    public ZipEntry MakeFileEntry(string fileName, bool useFileSystem)
    {
      var result = new ZipEntry(nameTransform_.TransformFile(fileName));
      result.IsUnicodeText = true;

      var externalAttributes = 0;
      var useAttributes = false;

      FileInfo fi = null;
      if (useFileSystem)
      {
        fi = new FileInfo(fileName);
      }

      if ((fi != null) && fi.Exists)
      {
        result.DateTime = fi.LastWriteTime;

        result.Size = fi.Length;

        useAttributes = true;
        externalAttributes = ((int) fi.Attributes & getAttributes_);
      }

      if (useAttributes)
      {
        result.ExternalFileAttributes = externalAttributes;
      }

      return result;
    }
예제 #5
0
파일: ZipFile.cs 프로젝트: IntegralLee/fomm
    /// <summary>
    ///   Search for and read the central directory of a zip file filling the entries array.
    /// </summary>
    /// <exception cref="System.IO.IOException">
    ///   An i/o error occurs.
    /// </exception>
    /// <exception cref="ZipException">
    ///   The central directory is malformed or cannot be found
    /// </exception>
    private void ReadEntries()
    {
      // Search for the End Of Central Directory.  When a zip comment is
      // present the directory will start earlier
      // 
      // The search is limited to 64K which is the maximum size of a trailing comment field to aid speed.
      // This should be compatible with both SFX and ZIP files but has only been tested for Zip files
      // If a SFX file has the Zip data attached as a resource and there are other resources occuring later then
      // this could be invalid.
      // Could also speed this up by reading memory in larger blocks.      

      if (baseStream_.CanSeek == false)
      {
        throw new ZipException("ZipFile stream must be seekable");
      }

      var locatedEndOfCentralDir = LocateBlockWithSignature(ZipConstants.EndOfCentralDirectorySignature,
                                                            baseStream_.Length, ZipConstants.EndOfCentralRecordBaseSize,
                                                            0xffff);

      if (locatedEndOfCentralDir < 0)
      {
        throw new ZipException("Cannot find central directory");
      }

      // Read end of central directory record
      var thisDiskNumber = ReadLEUshort();
      var startCentralDirDisk = ReadLEUshort();
      ulong entriesForThisDisk = ReadLEUshort();
      ulong entriesForWholeCentralDir = ReadLEUshort();
      ulong centralDirSize = ReadLEUint();
      long offsetOfCentralDir = ReadLEUint();
      uint commentSize = ReadLEUshort();

      if (commentSize > 0)
      {
        var comment = new byte[commentSize];

        StreamUtils.ReadFully(baseStream_, comment);
        comment_ = ZipConstants.ConvertToString(comment);
      }
      else
      {
        comment_ = string.Empty;
      }

      var isZip64 = false;

      // Check if zip64 header information is required.
      if ((thisDiskNumber == 0xffff) ||
          (startCentralDirDisk == 0xffff) ||
          (entriesForThisDisk == 0xffff) ||
          (entriesForWholeCentralDir == 0xffff) ||
          (centralDirSize == 0xffffffff) ||
          (offsetOfCentralDir == 0xffffffff))
      {
        isZip64 = true;

        var offset = LocateBlockWithSignature(ZipConstants.Zip64CentralDirLocatorSignature, locatedEndOfCentralDir, 0,
                                              0x1000);
        if (offset < 0)
        {
          throw new ZipException("Cannot find Zip64 locator");
        }

        // number of the disk with the start of the zip64 end of central directory 4 bytes 
        // relative offset of the zip64 end of central directory record 8 bytes 
        // total number of disks 4 bytes 
        ReadLEUint(); // startDisk64 is not currently used
        var offset64 = ReadLEUlong();
        ReadLEUint();

        baseStream_.Position = (long) offset64;
        long sig64 = ReadLEUint();

        if (sig64 != ZipConstants.Zip64CentralFileHeaderSignature)
        {
          throw new ZipException(string.Format("Invalid Zip64 Central directory signature at {0:X}", offset64));
        }

        // NOTE: Record size = SizeOfFixedFields + SizeOfVariableData - 12.
        ReadLEUlong();
        ReadLEUshort();
        ReadLEUshort();
        ReadLEUint();
        ReadLEUint();
        entriesForThisDisk = ReadLEUlong();
        ReadLEUlong();
        centralDirSize = ReadLEUlong();
        offsetOfCentralDir = (long) ReadLEUlong();

        // NOTE: zip64 extensible data sector (variable size) is ignored.
      }

      entries_ = new ZipEntry[entriesForThisDisk];

      // SFX/embedded support, find the offset of the first entry vis the start of the stream
      // This applies to Zip files that are appended to the end of an SFX stub.
      // Or are appended as a resource to an executable.
      // Zip files created by some archivers have the offsets altered to reflect the true offsets
      // and so dont require any adjustment here...
      // TODO: Difficulty with Zip64 and SFX offset handling needs resolution - maths?
      if (!isZip64 && (offsetOfCentralDir < locatedEndOfCentralDir - (4 + (long) centralDirSize)))
      {
        offsetOfFirstEntry = locatedEndOfCentralDir - (4 + (long) centralDirSize + offsetOfCentralDir);
        if (offsetOfFirstEntry <= 0)
        {
          throw new ZipException("Invalid embedded zip archive");
        }
      }

      baseStream_.Seek(offsetOfFirstEntry + offsetOfCentralDir, SeekOrigin.Begin);

      for (ulong i = 0; i < entriesForThisDisk; i++)
      {
        if (ReadLEUint() != ZipConstants.CentralHeaderSignature)
        {
          throw new ZipException("Wrong Central Directory signature");
        }

        int versionMadeBy = ReadLEUshort();
        int versionToExtract = ReadLEUshort();
        int bitFlags = ReadLEUshort();
        int method = ReadLEUshort();
        var dostime = ReadLEUint();
        var crc = ReadLEUint();
        var csize = (long) ReadLEUint();
        var size = (long) ReadLEUint();
        int nameLen = ReadLEUshort();
        int extraLen = ReadLEUshort();
        int commentLen = ReadLEUshort();

        ReadLEUshort(); // Not currently used
        ReadLEUshort(); // Not currently used

        var externalAttributes = ReadLEUint();
        long offset = ReadLEUint();

        var buffer = new byte[Math.Max(nameLen, commentLen)];

        StreamUtils.ReadFully(baseStream_, buffer, 0, nameLen);
        var name = ZipConstants.ConvertToStringExt(bitFlags, buffer, nameLen);

        var entry = new ZipEntry(name, versionToExtract, versionMadeBy, (CompressionMethod) method);
        entry.Crc = crc & 0xffffffffL;
        entry.Size = size & 0xffffffffL;
        entry.CompressedSize = csize & 0xffffffffL;
        entry.Flags = bitFlags;
        entry.DosTime = dostime;
        entry.ZipFileIndex = (long) i;
        entry.Offset = offset;
        entry.ExternalFileAttributes = (int) externalAttributes;

        if (extraLen > 0)
        {
          var extra = new byte[extraLen];
          StreamUtils.ReadFully(baseStream_, extra);
          entry.ExtraData = extra;
        }

        entry.ProcessExtraData(false);

        if (commentLen > 0)
        {
          StreamUtils.ReadFully(baseStream_, buffer, 0, commentLen);
          entry.Comment = ZipConstants.ConvertToStringExt(bitFlags, buffer, commentLen);
        }

        entries_[i] = entry;
      }
    }
예제 #6
0
파일: ZipFile.cs 프로젝트: IntegralLee/fomm
    /* Modify not yet ready for public consumption.
   Direct modification of an entry should not overwrite original data before its read.
   Safe mode is trivial in this sense.
    public void Modify(ZipEntry original, ZipEntry updated)
    {
      if ( original == null ) {
        throw new ArgumentNullException("original");
      }

      if ( updated == null ) {
        throw new ArgumentNullException("updated");
      }

      CheckUpdating();
      contentsEdited_ = true;
      updates_.Add(new ZipUpdate(original, updated));
    }
*/

    #endregion

    #region Deleting Entries

    /// <summary>
    ///   Delete a <see cref="ZipEntry" /> from the archive.
    /// </summary>
    /// <param name="entry">The entry to delete.</param>
    public void Delete(ZipEntry entry)
    {
      if (entry == null)
      {
        throw new ArgumentNullException("entry");
      }

      CheckUpdating();

      var index = FindExistingUpdate(entry);
      if (index >= 0)
      {
        contentsEdited_ = true;
        updates_[index] = null;
        updateCount_ -= 1;
      }
      else
      {
        throw new ZipException("Cannot find entry to delete");
      }
    }
예제 #7
0
파일: FastZip.cs 프로젝트: IntegralLee/fomm
 private void ExtractFileEntry(ZipEntry entry, string targetName)
 {
   if (continueRunning_)
   {
     try
     {
       using (var outputStream = File.Create(targetName))
       {
         if (buffer_ == null)
         {
           buffer_ = new byte[4096];
         }
         StreamUtils.Copy(zipFile_.GetInputStream(entry), outputStream, buffer_);
       }
       File.SetLastWriteTime(targetName, entry.DateTime);
     }
     catch (Exception)
     {
       continueRunning_ = false;
       throw;
     }
   }
 }
예제 #8
0
파일: ZipFile.cs 프로젝트: IntegralLee/fomm
    /// <summary>
    ///   Get an output stream for the specified <see cref="ZipEntry" />
    /// </summary>
    /// <param name="entry">The entry to get an output stream for.</param>
    /// <returns>The output stream obtained for the entry.</returns>
    private Stream GetOutputStream(ZipEntry entry)
    {
      var result = baseStream_;

      switch (entry.CompressionMethod)
      {
        case CompressionMethod.Stored:
          result = new UncompressedStream(result);
          break;

        case CompressionMethod.Deflated:
          var dos = new DeflaterOutputStream(result, new Deflater(9, true));
          dos.IsStreamOwner = false;
          result = dos;
          break;

        default:
          throw new ZipException("Unknown compression method " + entry.CompressionMethod);
      }
      return result;
    }
예제 #9
0
파일: ZipFile.cs 프로젝트: IntegralLee/fomm
 public ZipUpdate(IStaticDataSource dataSource, ZipEntry entry)
 {
   command_ = UpdateCommand.Add;
   Entry = entry;
   dataSource_ = dataSource;
 }
예제 #10
0
파일: ZipFile.cs 프로젝트: IntegralLee/fomm
    private int FindExistingUpdate(ZipEntry entry)
    {
      var result = -1;
      var convertedName = GetTransformedFileName(entry.Name);

      if (updateIndex_.ContainsKey(convertedName))
      {
        result = (int) updateIndex_[convertedName];
      }
      /*
      // This is slow like the coming of the next ice age but takes less storage and may be useful
      // for CF?
      for (int index = 0; index < updates_.Count; ++index)
      {
        ZipUpdate zu = ( ZipUpdate )updates_[index];
        if ( (zu.Entry.ZipFileIndex == entry.ZipFileIndex) &&
          (string.Compare(convertedName, zu.Entry.Name, true, CultureInfo.InvariantCulture) == 0) ) {
          result = index;
          break;
        }
      }
 */
      return result;
    }
예제 #11
0
파일: ZipFile.cs 프로젝트: IntegralLee/fomm
 /// <summary>
 ///   Initialises a default <see cref="ZipFile" /> instance with no entries and no file storage.
 /// </summary>
 internal ZipFile()
 {
   entries_ = new ZipEntry[0];
   IsNewArchive = true;
 }
예제 #12
0
파일: ZipFile.cs 프로젝트: IntegralLee/fomm
    private int WriteCentralDirectoryHeader(ZipEntry entry)
    {
      if (entry.CompressedSize < 0)
      {
        throw new ZipException("Attempt to write central directory entry with unknown csize");
      }

      if (entry.Size < 0)
      {
        throw new ZipException("Attempt to write central directory entry with unknown size");
      }

      if (entry.Crc < 0)
      {
        throw new ZipException("Attempt to write central directory entry with unknown crc");
      }

      // Write the central file header
      WriteLEInt(ZipConstants.CentralHeaderSignature);

      // Version made by
      WriteLEShort(ZipConstants.VersionMadeBy);

      // Version required to extract
      WriteLEShort(entry.Version);

      WriteLEShort(entry.Flags);

      unchecked
      {
        WriteLEShort((byte) entry.CompressionMethod);
        WriteLEInt((int) entry.DosTime);
        WriteLEInt((int) entry.Crc);
      }

      if ((entry.IsZip64Forced()) || (entry.CompressedSize >= 0xffffffff))
      {
        WriteLEInt(-1);
      }
      else
      {
        WriteLEInt((int) (entry.CompressedSize & 0xffffffff));
      }

      if ((entry.IsZip64Forced()) || (entry.Size >= 0xffffffff))
      {
        WriteLEInt(-1);
      }
      else
      {
        WriteLEInt((int) entry.Size);
      }

      var name = ZipConstants.ConvertToArray(entry.Flags, entry.Name);

      if (name.Length > 0xFFFF)
      {
        throw new ZipException("Entry name is too long.");
      }

      WriteLEShort(name.Length);

      // Central header extra data is different to local header version so regenerate.
      var ed = new ZipExtraData(entry.ExtraData);

      if (entry.CentralHeaderRequiresZip64)
      {
        ed.StartNewEntry();

        if ((entry.Size >= 0xffffffff) || (useZip64_ == UseZip64.On))
        {
          ed.AddLeLong(entry.Size);
        }

        if ((entry.CompressedSize >= 0xffffffff) || (useZip64_ == UseZip64.On))
        {
          ed.AddLeLong(entry.CompressedSize);
        }

        if (entry.Offset >= 0xffffffff)
        {
          ed.AddLeLong(entry.Offset);
        }

        // Number of disk on which this file starts isnt supported and is never written here.
        ed.AddNewEntry(1);
      }
      else
      {
        // Should have already be done when local header was added.
        ed.Delete(1);
      }

      var centralExtraData = ed.GetEntryData();

      WriteLEShort(centralExtraData.Length);
      WriteLEShort(entry.Comment != null ? entry.Comment.Length : 0);

      WriteLEShort(0); // disk number
      WriteLEShort(0); // internal file attributes

      // External file attributes...
      if (entry.ExternalFileAttributes != -1)
      {
        WriteLEInt(entry.ExternalFileAttributes);
      }
      else
      {
        WriteLEUint(entry.IsDirectory ? (uint) 16 : 0);
      }

      if (entry.Offset >= 0xffffffff)
      {
        WriteLEUint(0xffffffff);
      }
      else
      {
        WriteLEUint((uint) (int) entry.Offset);
      }

      if (name.Length > 0)
      {
        baseStream_.Write(name, 0, name.Length);
      }

      if (centralExtraData.Length > 0)
      {
        baseStream_.Write(centralExtraData, 0, centralExtraData.Length);
      }

      var rawComment = (entry.Comment != null) ? Encoding.ASCII.GetBytes(entry.Comment) : new byte[0];

      if (rawComment.Length > 0)
      {
        baseStream_.Write(rawComment, 0, rawComment.Length);
      }

      return ZipConstants.CentralHeaderBaseSize + name.Length + centralExtraData.Length + rawComment.Length;
    }
예제 #13
0
    /// <summary>
    ///   Write a data descriptor.
    /// </summary>
    /// <param name="entry">The entry to write a descriptor for.</param>
    /// <returns>Returns the number of descriptor bytes written.</returns>
    public int WriteDataDescriptor(ZipEntry entry)
    {
      if (entry == null)
      {
        throw new ArgumentNullException("entry");
      }

      var result = 0;

      // Add data descriptor if flagged as required
      if ((entry.Flags & (int) GeneralBitFlags.Descriptor) != 0)
      {
        // The signature is not PKZIP originally but is now described as optional
        // in the PKZIP Appnote documenting trhe format.
        WriteLEInt(ZipConstants.DataDescriptorSignature);
        WriteLEInt(unchecked((int) (entry.Crc)));

        result += 8;

        if (entry.LocalHeaderRequiresZip64)
        {
          WriteLELong(entry.CompressedSize);
          WriteLELong(entry.Size);
          result += 16;
        }
        else
        {
          WriteLEInt((int) entry.CompressedSize);
          WriteLEInt((int) entry.Size);
          result += 8;
        }
      }

      return result;
    }
예제 #14
0
    /// <summary>
    ///   Make a new <see cref="ZipEntry"></see> for a directory.
    /// </summary>
    /// <param name="directoryName">The raw untransformed name for the new directory</param>
    /// <param name="useFileSystem">If true entry detail is retrieved from the file system if the file exists.</param>
    /// <returns>Returns a new <see cref="ZipEntry"></see> representing a directory.</returns>
    public ZipEntry MakeDirectoryEntry(string directoryName, bool useFileSystem)
    {
      var result = new ZipEntry(nameTransform_.TransformDirectory(directoryName));
      result.Size = 0;

      var externalAttributes = 0;

      DirectoryInfo di = null;

      if (useFileSystem)
      {
        di = new DirectoryInfo(directoryName);
      }

      if ((di != null) && di.Exists)
      {
        result.DateTime = di.LastWriteTime;

        externalAttributes = ((int) di.Attributes & getAttributes_);
      }

      // Always set directory attribute on.
      externalAttributes |= 16;
      result.ExternalFileAttributes = externalAttributes;

      return result;
    }
예제 #15
0
파일: ZipFile.cs 프로젝트: IntegralLee/fomm
    /// <summary>
    ///   Gets an input stream for reading the given zip entry data in an uncompressed form.
    ///   Normally the <see cref="ZipEntry" /> should be an entry returned by GetEntry().
    /// </summary>
    /// <param name="entry">The <see cref="ZipEntry" /> to obtain a data <see cref="Stream" /> for</param>
    /// <returns>An input <see cref="Stream" /> containing data for this <see cref="ZipEntry" /></returns>
    /// <exception cref="ObjectDisposedException">
    ///   The ZipFile has already been closed
    /// </exception>
    /// <exception cref="ZipException">
    ///   The compression method for the entry is unknown
    /// </exception>
    /// <exception cref="IndexOutOfRangeException">
    ///   The entry is not found in the ZipFile
    /// </exception>
    public Stream GetInputStream(ZipEntry entry)
    {
      if (entry == null)
      {
        throw new ArgumentNullException("entry");
      }

      if (isDisposed_)
      {
        throw new ObjectDisposedException("ZipFile");
      }

      var index = entry.ZipFileIndex;
      if ((index < 0) || (index >= entries_.Length) || (entries_[index].Name != entry.Name))
      {
        index = FindEntry(entry.Name, true);
        if (index < 0)
        {
          throw new ZipException("Entry cannot be found");
        }
      }
      return GetInputStream(index);
    }
예제 #16
0
파일: ZipFile.cs 프로젝트: IntegralLee/fomm
 public ZipUpdate(UpdateCommand command, ZipEntry entry)
 {
   command_ = command;
   Entry = (ZipEntry) entry.Clone();
 }
예제 #17
0
파일: ZipFile.cs 프로젝트: IntegralLee/fomm
    /// <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>
    private long TestLocalHeader(ZipEntry entry, HeaderTest tests)
    {
      lock (baseStream_)
      {
        var testHeader = (tests & HeaderTest.Header) != 0;
        var 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));
        }

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

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

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

        var localExtraData = new ZipExtraData(extraData);

        // Extra data / zip64 checks
        if (localExtraData.Find(1))
        {
          // TODO Check for tag values being distinct..  Multiple zip64 tags means what?

          // 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 = 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.
          var 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 (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)))
        {
          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));
          }
        }

        var extraLength = storedNameLength + extraDataLength;
        return offsetOfFirstEntry + entry.Offset + ZipConstants.LocalHeaderBaseSize + extraLength;
      }
    }
예제 #18
0
파일: ZipFile.cs 프로젝트: IntegralLee/fomm
 /// <summary>
 ///   Copy an existing entry.
 /// </summary>
 /// <param name="entry">The existing entry to copy.</param>
 public ZipUpdate(ZipEntry entry)
   : this(UpdateCommand.Copy, entry)
 {
   // Do nothing.
 }
예제 #19
0
파일: FastZip.cs 프로젝트: IntegralLee/fomm
 //public event EventHandler<ZipEventArgs> EntryAdded;
 /// <summary>
 ///   Raised when a <see cref="ZipEntry" /> has been added to an archive.
 /// </summary>
 /// <summary>
 ///   Raises the <see cref="EntryExtracted" /> event.
 /// </summary>
 /// <param name="p_zeEntry">The <see cref="ZipEntry" /> that was extracted.</param>
 protected void OnEntryExtracted(ZipEntry p_zeEntry)
 {
   if (EntryExtracted != null)
   {
     var zeaArgs = new ZipEventArgs(p_zeEntry.Name);
     EntryExtracted(this, zeaArgs);
     continueRunning_ &= !zeaArgs.Cancel;
   }
 }
예제 #20
0
파일: ZipFile.cs 프로젝트: IntegralLee/fomm
    private void DisposeInternal()
    {
      if (!isDisposed_)
      {
        isDisposed_ = true;
        entries_ = new ZipEntry[0];

        if (IsStreamOwner && (baseStream_ != null))
        {
          lock (baseStream_)
          {
            baseStream_.Close();
          }
        }

        PostUpdateCleanup();
      }
    }
예제 #21
0
파일: FastZip.cs 프로젝트: IntegralLee/fomm
    private void ExtractEntry(ZipEntry entry)
    {
      var doExtraction = entry.IsCompressionMethodSupported();
      var 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)
      {
        dirName = entry.IsDirectory ? targetName : Path.GetDirectoryName(Path.GetFullPath(targetName));
      }

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

      if (doExtraction && entry.IsFile)
      {
        ExtractFileEntry(entry, targetName);
      }
      OnEntryExtracted(entry);
    }
예제 #22
0
    /// <summary>
    ///   Starts a new Zip entry. It automatically closes the previous
    ///   entry if present.
    ///   All entry elements bar name are optional, but must be correct if present.
    ///   If the compression method is stored and the output is not patchable
    ///   the compression for that entry is automatically changed to deflate level 0
    /// </summary>
    /// <param name="entry">
    ///   the entry.
    /// </param>
    /// <exception cref="System.ArgumentNullException">
    ///   if entry passed is null.
    /// </exception>
    /// <exception cref="System.IO.IOException">
    ///   if an I/O error occured.
    /// </exception>
    /// <exception cref="System.InvalidOperationException">
    ///   if stream was finished
    /// </exception>
    /// <exception cref="ZipException">
    ///   Too many entries in the Zip file<br />
    ///   Entry name is too long<br />
    ///   Finish has already been called<br />
    /// </exception>
    public void PutNextEntry(ZipEntry entry)
    {
      if (entry == null)
      {
        throw new ArgumentNullException("entry");
      }

      if (entries == null)
      {
        throw new InvalidOperationException("ZipOutputStream was finished");
      }

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

      if (entries.Count == int.MaxValue)
      {
        throw new ZipException("Too many entries for Zip file");
      }

      var method = entry.CompressionMethod;
      var compressionLevel = defaultCompressionLevel;

      // Clear flags that the library manages internally
      entry.Flags &= (int) GeneralBitFlags.UnicodeText;
      patchEntryHeader = false;

      bool headerInfoAvailable;

      // No need to compress - definitely no data.
      if (entry.Size == 0)
      {
        entry.CompressedSize = entry.Size;
        entry.Crc = 0;
        method = CompressionMethod.Stored;
        headerInfoAvailable = true;
      }
      else
      {
        headerInfoAvailable = (entry.Size >= 0) && entry.HasCrc;

        // Switch to deflation if storing isnt possible.
        if (method == CompressionMethod.Stored)
        {
          if (!headerInfoAvailable)
          {
            if (!CanPatchEntries)
            {
              // Can't patch entries so storing is not possible.
              method = CompressionMethod.Deflated;
              compressionLevel = 0;
            }
          }
          else // entry.size must be > 0
          {
            entry.CompressedSize = entry.Size;
            headerInfoAvailable = entry.HasCrc;
          }
        }
      }

      if (headerInfoAvailable == false)
      {
        if (CanPatchEntries == false)
        {
          // Only way to record size and compressed size is to append a data descriptor
          // after compressed data.

          // Stored entries of this form have already been converted to deflating.
          entry.Flags |= 8;
        }
        else
        {
          patchEntryHeader = true;
        }
      }

      entry.Offset = offset;
      entry.CompressionMethod = method;

      curMethod = method;
      sizePatchPos = -1;

      if ((useZip64_ == UseZip64.On) || ((entry.Size < 0) && (useZip64_ == UseZip64.Dynamic)))
      {
        entry.ForceZip64();
      }

      // Write the local file header
      WriteLeInt(ZipConstants.LocalHeaderSignature);

      WriteLeShort(entry.Version);
      WriteLeShort(entry.Flags);
      WriteLeShort((byte) method);
      WriteLeInt((int) entry.DosTime);

      // TODO: Refactor header writing.  Its done in several places.
      if (headerInfoAvailable)
      {
        WriteLeInt((int) entry.Crc);
        if (entry.LocalHeaderRequiresZip64)
        {
          WriteLeInt(-1);
          WriteLeInt(-1);
        }
        else
        {
          WriteLeInt((int) entry.CompressedSize);
          WriteLeInt((int) entry.Size);
        }
      }
      else
      {
        if (patchEntryHeader)
        {
          crcPatchPos = baseOutputStream_.Position;
        }
        WriteLeInt(0); // Crc

        if (patchEntryHeader)
        {
          sizePatchPos = baseOutputStream_.Position;
        }

        // For local header both sizes appear in Zip64 Extended Information
        if (entry.LocalHeaderRequiresZip64 || patchEntryHeader)
        {
          WriteLeInt(-1);
          WriteLeInt(-1);
        }
        else
        {
          WriteLeInt(0); // Compressed size
          WriteLeInt(0); // Uncompressed size
        }
      }

      var name = ZipConstants.ConvertToArray(entry.Flags, entry.Name);

      if (name.Length > 0xFFFF)
      {
        throw new ZipException("Entry name too long.");
      }

      var ed = new ZipExtraData(entry.ExtraData);

      if (entry.LocalHeaderRequiresZip64)
      {
        ed.StartNewEntry();
        if (headerInfoAvailable)
        {
          ed.AddLeLong(entry.Size);
          ed.AddLeLong(entry.CompressedSize);
        }
        else
        {
          ed.AddLeLong(-1);
          ed.AddLeLong(-1);
        }
        ed.AddNewEntry(1);

        if (!ed.Find(1))
        {
          throw new ZipException("Internal error cant find extra data");
        }

        if (patchEntryHeader)
        {
          sizePatchPos = ed.CurrentReadIndex;
        }
      }
      else
      {
        ed.Delete(1);
      }

      var extra = ed.GetEntryData();

      WriteLeShort(name.Length);
      WriteLeShort(extra.Length);

      if (name.Length > 0)
      {
        baseOutputStream_.Write(name, 0, name.Length);
      }

      if (entry.LocalHeaderRequiresZip64 && patchEntryHeader)
      {
        sizePatchPos += baseOutputStream_.Position;
      }

      if (extra.Length > 0)
      {
        baseOutputStream_.Write(extra, 0, extra.Length);
      }

      offset += ZipConstants.LocalHeaderBaseSize + name.Length + extra.Length;

      // Activate the entry.
      curEntry = entry;
      crc.Reset();
      if (method == CompressionMethod.Deflated)
      {
        deflater_.Reset();
        deflater_.SetLevel(compressionLevel);
      }
      size = 0;
    }