This class assists with writing/reading from Zip files.
Inheritance: Stream
 public byte[] GetData()
 {
     byte[] buffer;
     using (MemoryStream stream = new MemoryStream())
     {
         using (ZipHelperStream stream2 = new ZipHelperStream(stream))
         {
             stream2.IsStreamOwner = false;
             stream2.WriteByte((byte) this._flags);
             if (((byte) (this._flags & Flags.ModificationTime)) != 0)
             {
                 DateTime time = new DateTime(0x7b2, 1, 1, 0, 0, 0);
                 TimeSpan span = (TimeSpan) (this._modificationTime.ToUniversalTime() - time.ToUniversalTime());
                 int totalSeconds = (int) span.TotalSeconds;
                 stream2.WriteLEInt(totalSeconds);
             }
             if (((byte) (this._flags & Flags.AccessTime)) != 0)
             {
                 DateTime time2 = new DateTime(0x7b2, 1, 1, 0, 0, 0);
                 TimeSpan span2 = (TimeSpan) (this._lastAccessTime.ToUniversalTime() - time2.ToUniversalTime());
                 int num2 = (int) span2.TotalSeconds;
                 stream2.WriteLEInt(num2);
             }
             if (((byte) (this._flags & Flags.CreateTime)) != 0)
             {
                 DateTime time3 = new DateTime(0x7b2, 1, 1, 0, 0, 0);
                 TimeSpan span3 = (TimeSpan) (this._createTime.ToUniversalTime() - time3.ToUniversalTime());
                 int num3 = (int) span3.TotalSeconds;
                 stream2.WriteLEInt(num3);
             }
             buffer = stream.ToArray();
         }
     }
     return buffer;
 }
Exemple #2
0
 public void SetData(byte[] data, int index, int count)
 {
     using (MemoryStream stream = new MemoryStream(data, index, count, false))
     {
         using (ZipHelperStream stream2 = new ZipHelperStream(stream))
         {
             stream2.ReadLEInt();
             while (stream2.Position < stream2.Length)
             {
                 int num = stream2.ReadLEShort();
                 int num2 = stream2.ReadLEShort();
                 if (num == 1)
                 {
                     if (num2 >= 0x18)
                     {
                         long fileTime = stream2.ReadLELong();
                         this._lastModificationTime = DateTime.FromFileTime(fileTime);
                         long num4 = stream2.ReadLELong();
                         this._lastAccessTime = DateTime.FromFileTime(num4);
                         long num5 = stream2.ReadLELong();
                         this._createTime = DateTime.FromFileTime(num5);
                     }
                     return;
                 }
                 stream2.Seek((long) num2, SeekOrigin.Current);
             }
         }
     }
 }
 public void SetData(byte[] data, int index, int count)
 {
     using (MemoryStream stream = new MemoryStream(data, index, count, false))
     {
         using (ZipHelperStream stream2 = new ZipHelperStream(stream))
         {
             this._flags = (Flags) ((byte) stream2.ReadByte());
             if ((((byte) (this._flags & Flags.ModificationTime)) != 0) && (count >= 5))
             {
                 int seconds = stream2.ReadLEInt();
                 DateTime time = new DateTime(0x7b2, 1, 1, 0, 0, 0);
                 this._modificationTime = (time.ToUniversalTime() + new TimeSpan(0, 0, 0, seconds, 0)).ToLocalTime();
             }
             if (((byte) (this._flags & Flags.AccessTime)) != 0)
             {
                 int num2 = stream2.ReadLEInt();
                 DateTime time3 = new DateTime(0x7b2, 1, 1, 0, 0, 0);
                 this._lastAccessTime = (time3.ToUniversalTime() + new TimeSpan(0, 0, 0, num2, 0)).ToLocalTime();
             }
             if (((byte) (this._flags & Flags.CreateTime)) != 0)
             {
                 int num3 = stream2.ReadLEInt();
                 DateTime time5 = new DateTime(0x7b2, 1, 1, 0, 0, 0);
                 this._createTime = (time5.ToUniversalTime() + new TimeSpan(0, 0, 0, num3, 0)).ToLocalTime();
             }
         }
     }
 }
 /// <summary>
 /// Get the binary data representing this instance.
 /// </summary>
 /// <returns>The raw binary data representing this instance.</returns>
 public byte[] GetData()
 {
     using (MemoryStream ms = new MemoryStream())
         using (ZipHelperStream helperStream = new ZipHelperStream(ms))
         {
             helperStream.IsStreamOwner = false;
             helperStream.WriteLEInt(0);                   // Reserved
             helperStream.WriteLEShort(1);                 // Tag
             helperStream.WriteLEShort(24);                // Length = 3 x 8.
             helperStream.WriteLELong(_lastModificationTime.ToFileTime());
             helperStream.WriteLELong(_lastAccessTime.ToFileTime());
             helperStream.WriteLELong(_createTime.ToFileTime());
             return(ms.ToArray());
         }
 }
 public byte[] GetData()
 {
     using (MemoryStream memoryStream = new MemoryStream())
     {
         using (ZipHelperStream zipHelperStream = new ZipHelperStream(memoryStream))
         {
             zipHelperStream.IsStreamOwner = false;
             zipHelperStream.WriteLEInt(0);
             zipHelperStream.WriteLEShort(1);
             zipHelperStream.WriteLEShort(24);
             zipHelperStream.WriteLELong(_lastModificationTime.ToFileTime());
             zipHelperStream.WriteLELong(_lastAccessTime.ToFileTime());
             zipHelperStream.WriteLELong(_createTime.ToFileTime());
             return(memoryStream.ToArray());
         }
     }
 }
Exemple #6
0
 public byte[] GetData()
 {
     byte[] buffer;
     using (MemoryStream stream = new MemoryStream())
     {
         using (ZipHelperStream stream2 = new ZipHelperStream(stream))
         {
             stream2.IsStreamOwner = false;
             stream2.WriteLEInt(0);
             stream2.WriteLEShort(1);
             stream2.WriteLEShort(0x18);
             stream2.WriteLELong(this._lastModificationTime.ToFileTime());
             stream2.WriteLELong(this._lastAccessTime.ToFileTime());
             stream2.WriteLELong(this._createTime.ToFileTime());
             buffer = stream.ToArray();
         }
     }
     return buffer;
 }
Exemple #7
0
 public byte[] GetData()
 {
     byte[] buffer;
     using (MemoryStream stream = new MemoryStream())
     {
         using (ZipHelperStream stream2 = new ZipHelperStream(stream))
         {
             stream2.IsStreamOwner = false;
             stream2.WriteLEInt(0);
             stream2.WriteLEShort(1);
             stream2.WriteLEShort(0x18);
             stream2.WriteLELong(this.lastModificationTime_.ToFileTime());
             stream2.WriteLELong(this.lastAccessTime_.ToFileTime());
             stream2.WriteLELong(this.createTime_.ToFileTime());
             buffer = stream.ToArray();
         }
     }
     return(buffer);
 }
Exemple #8
0
        public void SetData(byte[] data, int index, int count)
        {
            //IL_0004: Unknown result type (might be due to invalid IL or missing references)
            //IL_000a: Expected O, but got Unknown
            MemoryStream val = new MemoryStream(data, index, count, false);

            try
            {
                ZipHelperStream zipHelperStream = new ZipHelperStream((Stream)(object)val);
                try
                {
                    zipHelperStream.ReadLEInt();
                    while (((Stream)zipHelperStream).get_Position() < ((Stream)zipHelperStream).get_Length())
                    {
                        int num  = zipHelperStream.ReadLEShort();
                        int num2 = zipHelperStream.ReadLEShort();
                        if (num == 1)
                        {
                            if (num2 >= 24)
                            {
                                long num3 = zipHelperStream.ReadLELong();
                                _lastModificationTime = global::System.DateTime.FromFileTime(num3);
                                long num4 = zipHelperStream.ReadLELong();
                                _lastAccessTime = global::System.DateTime.FromFileTime(num4);
                                long num5 = zipHelperStream.ReadLELong();
                                _createTime = global::System.DateTime.FromFileTime(num5);
                            }
                            break;
                        }
                        ((Stream)zipHelperStream).Seek((long)num2, (SeekOrigin)1);
                    }
                }
                finally
                {
                    ((global::System.IDisposable)zipHelperStream)?.Dispose();
                }
            }
            finally
            {
                ((global::System.IDisposable)val)?.Dispose();
            }
        }
Exemple #9
0
        public void SetData(byte[] data, int index, int count)
        {
            //IL_0004: Unknown result type (might be due to invalid IL or missing references)
            //IL_000a: Expected O, but got Unknown
            //IL_0052: Unknown result type (might be due to invalid IL or missing references)
            //IL_009a: Unknown result type (might be due to invalid IL or missing references)
            //IL_00e4: Unknown result type (might be due to invalid IL or missing references)
            MemoryStream val = new MemoryStream(data, index, count, false);

            try
            {
                ZipHelperStream zipHelperStream = new ZipHelperStream((Stream)(object)val);
                try
                {
                    _flags = (Flags)((Stream)zipHelperStream).ReadByte();
                    if ((_flags & Flags.ModificationTime) != 0 && count >= 5)
                    {
                        int num = zipHelperStream.ReadLEInt();
                        _modificationTime = (new global::System.DateTime(1970, 1, 1, 0, 0, 0).ToUniversalTime() + new TimeSpan(0, 0, 0, num, 0)).ToLocalTime();
                    }
                    if ((_flags & Flags.AccessTime) != 0)
                    {
                        int num2 = zipHelperStream.ReadLEInt();
                        _lastAccessTime = (new global::System.DateTime(1970, 1, 1, 0, 0, 0).ToUniversalTime() + new TimeSpan(0, 0, 0, num2, 0)).ToLocalTime();
                    }
                    if ((_flags & Flags.CreateTime) != 0)
                    {
                        int num3 = zipHelperStream.ReadLEInt();
                        _createTime = (new global::System.DateTime(1970, 1, 1, 0, 0, 0).ToUniversalTime() + new TimeSpan(0, 0, 0, num3, 0)).ToLocalTime();
                    }
                }
                finally
                {
                    ((global::System.IDisposable)zipHelperStream)?.Dispose();
                }
            }
            finally
            {
                ((global::System.IDisposable)val)?.Dispose();
            }
        }
Exemple #10
0
        /// <summary>
        /// Set the data from the raw values provided.
        /// </summary>
        /// <param name="data">The raw data to extract values from.</param>
        /// <param name="index">The index to start extracting values from.</param>
        /// <param name="count">The number of bytes available.</param>
        public void SetData(byte[] data, int index, int count)
        {
            using (MemoryStream ms = new MemoryStream(data, index, count, false))
                using (ZipHelperStream helperStream = new ZipHelperStream(ms))
                {
                    // bit 0           if set, modification time is present
                    // bit 1           if set, access time is present
                    // bit 2           if set, creation time is present

                    _flags = (Flags)helperStream.ReadByte();
                    if (((_flags & Flags.ModificationTime) != 0))
                    {
                        int iTime = helperStream.ReadLEInt();

                        _modificationTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc) +
                                            new TimeSpan(0, 0, 0, iTime, 0);

                        // Central-header version is truncated after modification time
                        if (count <= 5)
                        {
                            return;
                        }
                    }

                    if ((_flags & Flags.AccessTime) != 0)
                    {
                        int iTime = helperStream.ReadLEInt();

                        _lastAccessTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc) +
                                          new TimeSpan(0, 0, 0, iTime, 0);
                    }

                    if ((_flags & Flags.CreateTime) != 0)
                    {
                        int iTime = helperStream.ReadLEInt();

                        _createTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc) +
                                      new TimeSpan(0, 0, 0, iTime, 0);
                    }
                }
        }
 public byte[] GetData()
 {
     byte[] buffer;
     using (MemoryStream stream = new MemoryStream())
     {
         using (ZipHelperStream stream2 = new ZipHelperStream(stream))
         {
             TimeSpan span;
             int      totalSeconds;
             DateTime time;
             stream2.IsStreamOwner = false;
             stream2.WriteByte((byte)this._flags);
             if (((byte)(this._flags & Flags.ModificationTime)) != 0)
             {
                 time         = new DateTime(0x7b2, 1, 1, 0, 0, 0);
                 span         = (TimeSpan)(this._modificationTime.ToUniversalTime() - time.ToUniversalTime());
                 totalSeconds = (int)span.TotalSeconds;
                 stream2.WriteLEInt(totalSeconds);
             }
             if (((byte)(this._flags & Flags.AccessTime)) != 0)
             {
                 time         = new DateTime(0x7b2, 1, 1, 0, 0, 0);
                 span         = (TimeSpan)(this._lastAccessTime.ToUniversalTime() - time.ToUniversalTime());
                 totalSeconds = (int)span.TotalSeconds;
                 stream2.WriteLEInt(totalSeconds);
             }
             if (((byte)(this._flags & Flags.CreateTime)) != 0)
             {
                 time         = new DateTime(0x7b2, 1, 1, 0, 0, 0);
                 span         = (TimeSpan)(this._createTime.ToUniversalTime() - time.ToUniversalTime());
                 totalSeconds = (int)span.TotalSeconds;
                 stream2.WriteLEInt(totalSeconds);
             }
             buffer = stream.ToArray();
         }
     }
     return(buffer);
 }
Exemple #12
0
 public byte[] GetData()
 {
     byte[] result;
     using (MemoryStream memoryStream = new MemoryStream())
     {
         using (ZipHelperStream zipHelperStream = new ZipHelperStream(memoryStream))
         {
             zipHelperStream.IsStreamOwner = false;
             zipHelperStream.WriteLEInt(0);
             zipHelperStream.WriteLEShort(1);
             zipHelperStream.WriteLEShort(24);
             zipHelperStream.WriteLELong(this._lastModificationTime.ToFileTime());
             zipHelperStream.WriteLELong(this._lastAccessTime.ToFileTime());
             zipHelperStream.WriteLELong(this._createTime.ToFileTime());
             result = memoryStream.ToArray();
         }
     }
     return result;
 }
Exemple #13
0
 public void SetData(byte[] data, int index, int count)
 {
     using (MemoryStream memoryStream = new MemoryStream(data, index, count, false))
     {
         using (ZipHelperStream zipHelperStream = new ZipHelperStream(memoryStream))
         {
             zipHelperStream.ReadLEInt();
             while (zipHelperStream.Position < zipHelperStream.Length)
             {
                 int num = zipHelperStream.ReadLEShort();
                 int num2 = zipHelperStream.ReadLEShort();
                 if (num == 1)
                 {
                     if (num2 >= 24)
                     {
                         long fileTime = zipHelperStream.ReadLELong();
                         this._lastModificationTime = DateTime.FromFileTime(fileTime);
                         long fileTime2 = zipHelperStream.ReadLELong();
                         this._lastAccessTime = DateTime.FromFileTime(fileTime2);
                         long fileTime3 = zipHelperStream.ReadLELong();
                         this._createTime = DateTime.FromFileTime(fileTime3);
                     }
                     break;
                 }
                 zipHelperStream.Seek((long)num2, SeekOrigin.Current);
             }
         }
     }
 }
Exemple #14
0
 private long LocateBlockWithSignature(int signature, long endLocation, int minimumBlockSize, int maximumVariableData)
 {
     using (ZipHelperStream stream = new ZipHelperStream(this.baseStream_))
     {
         return stream.LocateBlockWithSignature(signature, endLocation, minimumBlockSize, maximumVariableData);
     }
 }
Exemple #15
0
		void AddEntry(ZipFile workFile, ZipUpdate update)
		{
			Stream source = null;

			if ( update.Entry.IsFile ) {
				source = update.GetSource();
				
				if ( source == null ) {
					source = updateDataSource_.GetSource(update.Entry, update.Filename);
				}
			}

			if ( source != null ) {
				using ( source ) {
					long sourceStreamLength = source.Length;
					if ( update.OutEntry.Size < 0 ) {
						update.OutEntry.Size = sourceStreamLength;
					}
					else {
						// Check for errant entries.
						if ( update.OutEntry.Size != sourceStreamLength ) {
							throw new ZipException("Entry size/stream size mismatch");
						}
					}

					workFile.WriteLocalEntryHeader(update);

					long dataStart = workFile.baseStream_.Position;

					using ( Stream output = workFile.GetOutputStream(update.OutEntry) ) {
						CopyBytes(update, output, source, sourceStreamLength, true);
					}

					long dataEnd = workFile.baseStream_.Position;
					update.OutEntry.CompressedSize = dataEnd - dataStart;

					if ((update.OutEntry.Flags & (int)GeneralBitFlags.Descriptor) == (int)GeneralBitFlags.Descriptor)
					{
						ZipHelperStream helper = new ZipHelperStream(workFile.baseStream_);
						helper.WriteDataDescriptor(update.OutEntry);
					}
				}
			}
			else {
				workFile.WriteLocalEntryHeader(update);
				update.OutEntry.CompressedSize = 0;
			}

		}
Exemple #16
0
		/// <summary>
		/// Set the data from the raw values provided.
		/// </summary>
		/// <param name="data">The raw data to extract values from.</param>
		/// <param name="index">The index to start extracting values from.</param>
		/// <param name="count">The number of bytes available.</param>
		public void SetData(byte[] data, int index, int count)
		{
			using (MemoryStream ms = new MemoryStream(data, index, count, false)) 
			using (ZipHelperStream helperStream = new ZipHelperStream(ms))
			{
				helperStream.ReadLEInt(); // Reserved
				while (helperStream.Position < helperStream.Length)
				{
					int ntfsTag = helperStream.ReadLEShort();
					int ntfsLength = helperStream.ReadLEShort();
					if (ntfsTag == 1)
					{
						if (ntfsLength >= 24)
						{
							long lastModificationTicks = helperStream.ReadLELong();
							lastModificationTime_ = DateTime.FromFileTime(lastModificationTicks);

							long lastAccessTicks = helperStream.ReadLELong();
							lastAccessTime_ = DateTime.FromFileTime(lastAccessTicks);

							long createTimeTicks = helperStream.ReadLELong();
							createTime_ = DateTime.FromFileTime(createTimeTicks);
						}
						break;
					}
					else
					{
						// An unknown NTFS tag so simply skip it.
						helperStream.Seek(ntfsLength, SeekOrigin.Current);
					}
				}
			}
		}
Exemple #17
0
		/// <summary>
		/// Set the data from the raw values provided.
		/// </summary>
		/// <param name="data">The raw data to extract values from.</param>
		/// <param name="index">The index to start extracting values from.</param>
		/// <param name="count">The number of bytes available.</param>
		public void SetData(byte[] data, int index, int count)
		{
			using (MemoryStream ms = new MemoryStream(data, index, count, false))
			using (ZipHelperStream helperStream = new ZipHelperStream(ms))
			{
				// bit 0           if set, modification time is present
				// bit 1           if set, access time is present
				// bit 2           if set, creation time is present
				
				flags_ = (Flags)helperStream.ReadByte();
				if (((flags_ & Flags.ModificationTime) != 0) && (count >= 5))
				{
					int iTime = helperStream.ReadLEInt();

					modificationTime_ = (new System.DateTime(1970, 1, 1, 0, 0, 0).ToUniversalTime() +
						new TimeSpan(0, 0, 0, iTime, 0)).ToLocalTime();
				}

				if ((flags_ & Flags.AccessTime) != 0)
				{
					int iTime = helperStream.ReadLEInt();

					lastAccessTime_ = (new System.DateTime(1970, 1, 1, 0, 0, 0).ToUniversalTime() +
						new TimeSpan(0, 0, 0, iTime, 0)).ToLocalTime();
				}
				
				if ((flags_ & Flags.CreateTime) != 0)
				{
					int iTime = helperStream.ReadLEInt();

					createTime_ = (new System.DateTime(1970, 1, 1, 0, 0, 0).ToUniversalTime() +
						new TimeSpan(0, 0, 0, iTime, 0)).ToLocalTime();
				}
			}
		}
Exemple #18
0
		void RunUpdates()
		{
			long sizeEntries = 0;
			long endOfStream = 0;
			bool directUpdate = false;
			long destinationPosition = 0; // NOT SFX friendly

			ZipFile workFile;

			if ( IsNewArchive ) {
				workFile = this;
				workFile.baseStream_.Position = 0;
				directUpdate = true;
			}
			else if ( archiveStorage_.UpdateMode == FileUpdateMode.Direct ) {
				workFile = this;
				workFile.baseStream_.Position = 0;
				directUpdate = true;

				// Sort the updates by offset within copies/modifies, then adds.
				// This ensures that data required by copies will not be overwritten.
				updates_.Sort(new UpdateComparer());
			}
			else {
				workFile = ZipFile.Create(archiveStorage_.GetTemporaryOutput());
				workFile.UseZip64 = UseZip64;
				
				if (key != null) {
					workFile.key = (byte[])key.Clone();
				}
			}

			try {
				foreach ( ZipUpdate update in updates_ ) {
					if (update != null) {
						switch (update.Command) {
							case UpdateCommand.Copy:
								if (directUpdate) {
									CopyEntryDirect(workFile, update, ref destinationPosition);
								}
								else {
									CopyEntry(workFile, update);
								}
								break;

							case UpdateCommand.Modify:
								// TODO: Direct modifying of an entry will take some legwork.
								ModifyEntry(workFile, update);
								break;

							case UpdateCommand.Add:
								if (!IsNewArchive && directUpdate) {
									workFile.baseStream_.Position = destinationPosition;
								}

								AddEntry(workFile, update);

								if (directUpdate) {
									destinationPosition = workFile.baseStream_.Position;
								}
								break;
						}
					}
				}

				if ( !IsNewArchive && directUpdate ) {
					workFile.baseStream_.Position = destinationPosition;
				}

				long centralDirOffset = workFile.baseStream_.Position;

				foreach ( ZipUpdate update in updates_ ) {
					if (update != null) {
						sizeEntries += workFile.WriteCentralDirectoryHeader(update.OutEntry);
					}
				}

				byte[] theComment = (newComment_ != null) ? newComment_.RawComment : ZipConstants.ConvertToArray(comment_);
				using ( ZipHelperStream zhs = new ZipHelperStream(workFile.baseStream_) ) {
					zhs.WriteEndOfCentralDirectory(updateCount_, sizeEntries, centralDirOffset, theComment);
				}

				endOfStream = workFile.baseStream_.Position;

				// And now patch entries...
				foreach ( ZipUpdate update in updates_ ) {
					if (update != null)
					{
						// If the size of the entry is zero leave the crc as 0 as well.
						// The calculated crc will be all bits on...
						if ((update.CrcPatchOffset > 0) && (update.OutEntry.CompressedSize > 0)) {
							workFile.baseStream_.Position = update.CrcPatchOffset;
							workFile.WriteLEInt((int)update.OutEntry.Crc);
						}

						if (update.SizePatchOffset > 0) {
							workFile.baseStream_.Position = update.SizePatchOffset;
							if (update.OutEntry.LocalHeaderRequiresZip64) {
								workFile.WriteLeLong(update.OutEntry.Size);
								workFile.WriteLeLong(update.OutEntry.CompressedSize);
							}
							else {
								workFile.WriteLEInt((int)update.OutEntry.CompressedSize);
								workFile.WriteLEInt((int)update.OutEntry.Size);
							}
						}
					}
				}
			}
			catch {
				workFile.Close();
				if (!directUpdate && (workFile.Name != null)) {
					File.Delete(workFile.Name);
				}
				throw;
			}

			if (directUpdate) {
				workFile.baseStream_.SetLength(endOfStream);
				workFile.baseStream_.Flush();
				isNewArchive_ = false;
				ReadEntries();
			}
			else {
				baseStream_.Close();
				Reopen(archiveStorage_.ConvertTemporaryToFinal());
			}
		}
        void UpdateCommentOnly()
        {
            long baseLength = baseStream_.Length;

            ZipHelperStream updateFile = null;

            if ( archiveStorage_.UpdateMode == FileUpdateMode.Safe ) {
                Stream copyStream = archiveStorage_.MakeTemporaryCopy(baseStream_);
                updateFile = new ZipHelperStream(copyStream);
                updateFile.IsStreamOwner = true;

                baseStream_.Close();
                baseStream_ = null;
            }
            else {
                if (archiveStorage_.UpdateMode == FileUpdateMode.Direct) {

                    baseStream_ = archiveStorage_.OpenForDirectUpdate(baseStream_);
                    updateFile = new ZipHelperStream(baseStream_);
                }
                else {
                    baseStream_.Close();
                    baseStream_ = null;
                    updateFile = new ZipHelperStream(Name);
                }
            }

            using ( updateFile ) {
                long locatedCentralDirOffset =
                    updateFile.LocateBlockWithSignature(ZipConstants.EndOfCentralDirectorySignature,
                                                        baseLength, ZipConstants.EndOfCentralRecordBaseSize, 0xffff);
                if ( locatedCentralDirOffset < 0 ) {
                    throw new ZipException("Cannot find central directory");
                }

                const int CentralHeaderCommentSizeOffset = 16;
                updateFile.Position += CentralHeaderCommentSizeOffset;

                byte[] rawComment = newComment_.RawComment;

                updateFile.WriteLEShort(rawComment.Length);
                updateFile.Write(rawComment, 0, rawComment.Length);
                updateFile.SetLength(updateFile.Position);
            }

            if ( archiveStorage_.UpdateMode == FileUpdateMode.Safe ) {
                Reopen(archiveStorage_.ConvertTemporaryToFinal());
            }
            else {
                ReadEntries();
            }
        }
        void RunUpdates()
        {
            long sizeEntries = 0;
            long endOfStream = 0;
            bool allOk = true;
            bool directUpdate = false;
            long destinationPosition = 0;

            ZipFile workFile;

            if ( IsNewArchive ) {
                workFile = this;
                workFile.baseStream_.Position = 0;
                directUpdate = true;
            }
            else if ( archiveStorage_.UpdateMode == FileUpdateMode.Direct ) {
                workFile = this;
                workFile.baseStream_.Position = 0;
                directUpdate = true;

                updates_.Sort(new UpdateComparer());
            }
            else {
                workFile = ZipFile.Create(archiveStorage_.GetTemporaryOutput());
                workFile.UseZip64 = UseZip64;

                if (key != null) {
                    workFile.key = (byte[])key.Clone();
                }
            }

            try {
                foreach ( ZipUpdate update in updates_ ) {
                    if (update != null) {
                        switch (update.Command) {
                            case UpdateCommand.Copy:
                                if (directUpdate) {
                                    CopyEntryDirect(workFile, update, ref destinationPosition);
                                }
                                else {
                                    CopyEntry(workFile, update);
                                }
                                break;

                            case UpdateCommand.Modify:
                                ModifyEntry(workFile, update);
                                break;

                            case UpdateCommand.Add:
                                if (!IsNewArchive && directUpdate) {
                                    workFile.baseStream_.Position = destinationPosition;
                                }

                                AddEntry(workFile, update);

                                if (directUpdate) {
                                    destinationPosition = workFile.baseStream_.Position;
                                }
                                break;
                        }
                    }
                }

                if ( !IsNewArchive && directUpdate ) {
                    workFile.baseStream_.Position = destinationPosition;
                }

                long centralDirOffset = workFile.baseStream_.Position;

                foreach ( ZipUpdate update in updates_ ) {
                    if (update != null) {
                        sizeEntries += workFile.WriteCentralDirectoryHeader(update.OutEntry);
                    }
                }

                byte[] theComment = (newComment_ != null) ? newComment_.RawComment : ZipConstants.ConvertToArray(comment_);
                using ( ZipHelperStream zhs = new ZipHelperStream(workFile.baseStream_) ) {
                    zhs.WriteEndOfCentralDirectory(updateCount_, sizeEntries, centralDirOffset, theComment);
                }

                endOfStream = workFile.baseStream_.Position;

                foreach ( ZipUpdate update in updates_ ) {
                    if (update != null)
                    {
                        if ((update.CrcPatchOffset > 0) && (update.OutEntry.CompressedSize > 0)) {
                            workFile.baseStream_.Position = update.CrcPatchOffset;
                            workFile.WriteLEInt((int)update.OutEntry.Crc);
                        }

                        if (update.SizePatchOffset > 0) {
                            workFile.baseStream_.Position = update.SizePatchOffset;
                            if (update.OutEntry.LocalHeaderRequiresZip64) {
                                workFile.WriteLeLong(update.OutEntry.Size);
                                workFile.WriteLeLong(update.OutEntry.CompressedSize);
                            }
                            else {
                                workFile.WriteLEInt((int)update.OutEntry.CompressedSize);
                                workFile.WriteLEInt((int)update.OutEntry.Size);
                            }
                        }
                    }
                }
            }
            catch(Exception) {
                allOk = false;
            }
            finally {
                if ( directUpdate ) {
                    if ( allOk ) {
                        workFile.baseStream_.Flush();
                        workFile.baseStream_.SetLength(endOfStream);
                    }
                }
                else {
                    workFile.Close();
                }
            }

            if ( allOk ) {
                if ( directUpdate ) {
                    isNewArchive_ = false;
                    workFile.baseStream_.Flush();
                    ReadEntries();
                }
                else {
                    baseStream_.Close();
                    Reopen(archiveStorage_.ConvertTemporaryToFinal());
                }
            }
            else {
                workFile.Close();
                if ( !directUpdate && (workFile.Name != null) ) {
                    File.Delete(workFile.Name);
                }
            }
        }
Exemple #21
0
 private void UpdateCommentOnly()
 {
     long length = this.baseStream_.Length;
     ZipHelperStream stream = null;
     if (this.archiveStorage_.UpdateMode == FileUpdateMode.Safe)
     {
         stream = new ZipHelperStream(this.archiveStorage_.MakeTemporaryCopy(this.baseStream_)) {
             IsStreamOwner = true
         };
         this.baseStream_.Close();
         this.baseStream_ = null;
     }
     else if (this.archiveStorage_.UpdateMode == FileUpdateMode.Direct)
     {
         this.baseStream_ = this.archiveStorage_.OpenForDirectUpdate(this.baseStream_);
         stream = new ZipHelperStream(this.baseStream_);
     }
     else
     {
         this.baseStream_.Close();
         this.baseStream_ = null;
         stream = new ZipHelperStream(this.Name);
     }
     using (stream)
     {
         if (stream.LocateBlockWithSignature(0x6054b50, length, 0x16, 0xffff) < 0L)
         {
             throw new ZipException("Cannot find central directory");
         }
         stream.Position += 0x10L;
         byte[] rawComment = this.newComment_.RawComment;
         stream.WriteLEShort(rawComment.Length);
         stream.Write(rawComment, 0, rawComment.Length);
         stream.SetLength(stream.Position);
     }
     if (this.archiveStorage_.UpdateMode == FileUpdateMode.Safe)
     {
         this.Reopen(this.archiveStorage_.ConvertTemporaryToFinal());
     }
     else
     {
         this.ReadEntries();
     }
 }
Exemple #22
0
        public override void Finish()
        {
            if (entries == null)
            {
                return;
            }
            if (curEntry != null)
            {
                CloseEntry();
            }
            long noOfEntries = entries.get_Count();
            long num         = 0L;

            global::System.Collections.IEnumerator enumerator = entries.GetEnumerator();
            try
            {
                while (enumerator.MoveNext())
                {
                    ZipEntry zipEntry = (ZipEntry)enumerator.get_Current();
                    WriteLeInt(33639248);
                    WriteLeShort(51);
                    WriteLeShort(zipEntry.Version);
                    WriteLeShort(zipEntry.Flags);
                    WriteLeShort((short)zipEntry.CompressionMethodForHeader);
                    WriteLeInt((int)zipEntry.DosTime);
                    WriteLeInt((int)zipEntry.Crc);
                    if (zipEntry.IsZip64Forced() || zipEntry.CompressedSize >= 4294967295u)
                    {
                        WriteLeInt(-1);
                    }
                    else
                    {
                        WriteLeInt((int)zipEntry.CompressedSize);
                    }
                    if (zipEntry.IsZip64Forced() || zipEntry.Size >= 4294967295u)
                    {
                        WriteLeInt(-1);
                    }
                    else
                    {
                        WriteLeInt((int)zipEntry.Size);
                    }
                    byte[] array = ZipConstants.ConvertToArray(zipEntry.Flags, zipEntry.Name);
                    if (array.Length > 65535)
                    {
                        throw new ZipException("Name too long.");
                    }
                    ZipExtraData zipExtraData = new ZipExtraData(zipEntry.ExtraData);
                    if (zipEntry.CentralHeaderRequiresZip64)
                    {
                        zipExtraData.StartNewEntry();
                        if (zipEntry.IsZip64Forced() || zipEntry.Size >= 4294967295u)
                        {
                            zipExtraData.AddLeLong(zipEntry.Size);
                        }
                        if (zipEntry.IsZip64Forced() || zipEntry.CompressedSize >= 4294967295u)
                        {
                            zipExtraData.AddLeLong(zipEntry.CompressedSize);
                        }
                        if (zipEntry.Offset >= 4294967295u)
                        {
                            zipExtraData.AddLeLong(zipEntry.Offset);
                        }
                        zipExtraData.AddNewEntry(1);
                    }
                    else
                    {
                        zipExtraData.Delete(1);
                    }
                    byte[] entryData = zipExtraData.GetEntryData();
                    byte[] array2    = ((zipEntry.Comment != null) ? ZipConstants.ConvertToArray(zipEntry.Flags, zipEntry.Comment) : new byte[0]);
                    if (array2.Length > 65535)
                    {
                        throw new ZipException("Comment too long.");
                    }
                    WriteLeShort(array.Length);
                    WriteLeShort(entryData.Length);
                    WriteLeShort(array2.Length);
                    WriteLeShort(0);
                    WriteLeShort(0);
                    if (zipEntry.ExternalFileAttributes != -1)
                    {
                        WriteLeInt(zipEntry.ExternalFileAttributes);
                    }
                    else if (zipEntry.IsDirectory)
                    {
                        WriteLeInt(16);
                    }
                    else
                    {
                        WriteLeInt(0);
                    }
                    if (zipEntry.Offset >= 4294967295u)
                    {
                        WriteLeInt(-1);
                    }
                    else
                    {
                        WriteLeInt((int)zipEntry.Offset);
                    }
                    if (array.Length > 0)
                    {
                        baseOutputStream_.Write(array, 0, array.Length);
                    }
                    if (entryData.Length > 0)
                    {
                        baseOutputStream_.Write(entryData, 0, entryData.Length);
                    }
                    if (array2.Length > 0)
                    {
                        baseOutputStream_.Write(array2, 0, array2.Length);
                    }
                    num += 46 + array.Length + entryData.Length + array2.Length;
                }
            }
            finally
            {
                global::System.IDisposable disposable = enumerator as global::System.IDisposable;
                if (disposable != null)
                {
                    disposable.Dispose();
                }
            }
            ZipHelperStream zipHelperStream = new ZipHelperStream(baseOutputStream_);

            try
            {
                zipHelperStream.WriteEndOfCentralDirectory(noOfEntries, num, offset, zipComment);
            }
            finally
            {
                ((global::System.IDisposable)zipHelperStream)?.Dispose();
            }
            entries = null;
        }
Exemple #23
0
 public override void Finish()
 {
     if (entries != null)
     {
         if (curEntry != null)
         {
             CloseEntry();
         }
         long noOfEntries = entries.Count;
         long num         = 0L;
         foreach (ZipEntry entry in entries)
         {
             WriteLeInt(33639248);
             WriteLeShort(51);
             WriteLeShort(entry.Version);
             WriteLeShort(entry.Flags);
             WriteLeShort((short)entry.CompressionMethodForHeader);
             WriteLeInt((int)entry.DosTime);
             WriteLeInt((int)entry.Crc);
             if (entry.IsZip64Forced() || entry.CompressedSize >= uint.MaxValue)
             {
                 WriteLeInt(-1);
             }
             else
             {
                 WriteLeInt((int)entry.CompressedSize);
             }
             if (entry.IsZip64Forced() || entry.Size >= uint.MaxValue)
             {
                 WriteLeInt(-1);
             }
             else
             {
                 WriteLeInt((int)entry.Size);
             }
             byte[] array = ZipConstants.ConvertToArray(entry.Flags, entry.Name);
             if (array.Length > 65535)
             {
                 throw new ZipException("Name too long.");
             }
             ZipExtraData zipExtraData = new ZipExtraData(entry.ExtraData);
             if (entry.CentralHeaderRequiresZip64)
             {
                 zipExtraData.StartNewEntry();
                 if (entry.IsZip64Forced() || entry.Size >= uint.MaxValue)
                 {
                     zipExtraData.AddLeLong(entry.Size);
                 }
                 if (entry.IsZip64Forced() || entry.CompressedSize >= uint.MaxValue)
                 {
                     zipExtraData.AddLeLong(entry.CompressedSize);
                 }
                 if (entry.Offset >= uint.MaxValue)
                 {
                     zipExtraData.AddLeLong(entry.Offset);
                 }
                 zipExtraData.AddNewEntry(1);
             }
             else
             {
                 zipExtraData.Delete(1);
             }
             if (entry.AESKeySize > 0)
             {
                 AddExtraDataAES(entry, zipExtraData);
             }
             byte[] entryData = zipExtraData.GetEntryData();
             byte[] array2    = (entry.Comment != null) ? ZipConstants.ConvertToArray(entry.Flags, entry.Comment) : new byte[0];
             if (array2.Length > 65535)
             {
                 throw new ZipException("Comment too long.");
             }
             WriteLeShort(array.Length);
             WriteLeShort(entryData.Length);
             WriteLeShort(array2.Length);
             WriteLeShort(0);
             WriteLeShort(0);
             if (entry.ExternalFileAttributes != -1)
             {
                 WriteLeInt(entry.ExternalFileAttributes);
             }
             else if (entry.IsDirectory)
             {
                 WriteLeInt(16);
             }
             else
             {
                 WriteLeInt(0);
             }
             if (entry.Offset >= uint.MaxValue)
             {
                 WriteLeInt(-1);
             }
             else
             {
                 WriteLeInt((int)entry.Offset);
             }
             if (array.Length != 0)
             {
                 baseOutputStream_.Write(array, 0, array.Length);
             }
             if (entryData.Length != 0)
             {
                 baseOutputStream_.Write(entryData, 0, entryData.Length);
             }
             if (array2.Length != 0)
             {
                 baseOutputStream_.Write(array2, 0, array2.Length);
             }
             num += 46 + array.Length + entryData.Length + array2.Length;
         }
         using (ZipHelperStream zipHelperStream = new ZipHelperStream(baseOutputStream_))
         {
             zipHelperStream.WriteEndOfCentralDirectory(noOfEntries, num, offset, zipComment);
         }
         entries = null;
     }
 }
Exemple #24
0
 public bool TestArchive(bool testData, TestStrategy strategy, ZipTestResultHandler resultHandler)
 {
     if (this.isDisposed_)
     {
         throw new ObjectDisposedException("ZipFile");
     }
     TestStatus status = new TestStatus(this);
     if (resultHandler != null)
     {
         resultHandler(status, null);
     }
     HeaderTest tests = testData ? (HeaderTest.Header | HeaderTest.Extract) : HeaderTest.Header;
     bool flag = true;
     try
     {
         for (int i = 0; flag && (i < this.Count); i++)
         {
             if (resultHandler != null)
             {
                 status.SetEntry(this[i]);
                 status.SetOperation(TestOperation.EntryHeader);
                 resultHandler(status, null);
             }
             try
             {
                 this.TestLocalHeader(this[i], tests);
             }
             catch (ZipException exception)
             {
                 status.AddError();
                 if (resultHandler != null)
                 {
                     resultHandler(status, string.Format("Exception during test - '{0}'", exception.Message));
                 }
                 if (strategy == TestStrategy.FindFirstError)
                 {
                     flag = false;
                 }
             }
             if ((flag && testData) && this[i].IsFile)
             {
                 if (resultHandler != null)
                 {
                     status.SetOperation(TestOperation.EntryData);
                     resultHandler(status, null);
                 }
                 Crc32 crc = new Crc32();
                 using (Stream stream = this.GetInputStream(this[i]))
                 {
                     int num3;
                     byte[] buffer = new byte[0x1000];
                     long num2 = 0L;
                     while ((num3 = stream.Read(buffer, 0, buffer.Length)) > 0)
                     {
                         crc.Update(buffer, 0, num3);
                         if (resultHandler != null)
                         {
                             num2 += num3;
                             status.SetBytesTested(num2);
                             resultHandler(status, null);
                         }
                     }
                 }
                 if (this[i].Crc != crc.Value)
                 {
                     status.AddError();
                     if (resultHandler != null)
                     {
                         resultHandler(status, "CRC mismatch");
                     }
                     if (strategy == TestStrategy.FindFirstError)
                     {
                         flag = false;
                     }
                 }
                 if ((this[i].Flags & 8) != 0)
                 {
                     ZipHelperStream stream2 = new ZipHelperStream(this.baseStream_);
                     DescriptorData data = new DescriptorData();
                     stream2.ReadDataDescriptor(this[i].LocalHeaderRequiresZip64, data);
                     if (this[i].Crc != data.Crc)
                     {
                         status.AddError();
                     }
                     if (this[i].CompressedSize != data.CompressedSize)
                     {
                         status.AddError();
                     }
                     if (this[i].Size != data.Size)
                     {
                         status.AddError();
                     }
                 }
             }
             if (resultHandler != null)
             {
                 status.SetOperation(TestOperation.EntryComplete);
                 resultHandler(status, null);
             }
         }
         if (resultHandler != null)
         {
             status.SetOperation(TestOperation.MiscellaneousTests);
             resultHandler(status, null);
         }
     }
     catch (Exception exception2)
     {
         status.AddError();
         if (resultHandler != null)
         {
             resultHandler(status, string.Format("Exception during test - '{0}'", exception2.Message));
         }
     }
     if (resultHandler != null)
     {
         status.SetOperation(TestOperation.Complete);
         status.SetEntry(null);
         resultHandler(status, null);
     }
     return (status.ErrorCount == 0);
 }
Exemple #25
0
		/// <summary>
		/// Test an archive for integrity/validity
		/// </summary>
		/// <param name="testData">Perform low level data Crc check</param>
		/// <param name="strategy">The <see cref="TestStrategy"></see> to apply.</param>
		/// <param name="resultHandler">The <see cref="ZipTestResultHandler"></see> handler to call during testing.</param>
		/// <returns>true if all tests pass, false otherwise</returns>
		/// <exception cref="ObjectDisposedException">The object has already been closed.</exception>
		public bool TestArchive(bool testData, TestStrategy strategy, ZipTestResultHandler resultHandler)
		{
			if (isDisposed_) {
				throw new ObjectDisposedException("ZipFile");
			}
			
			TestStatus status = new TestStatus(this);

			if ( resultHandler != null ) {
				resultHandler(status, null);
			}

			HeaderTest test = testData ? (HeaderTest.Header | HeaderTest.Extract) : HeaderTest.Header;

			bool testing = true;

			try {
				int entryIndex = 0;

				while ( testing && (entryIndex < Count) ) {
					if ( resultHandler != null ) {
						status.SetEntry(this[entryIndex]);
						status.SetOperation(TestOperation.EntryHeader);
						resultHandler(status, null);
					}

					try	{
						TestLocalHeader(this[entryIndex], test);
					}
					catch(ZipException ex) {
						status.AddError();

						if ( resultHandler != null ) {
							resultHandler(status,
								string.Format("Exception during test - '{0}'", ex.Message));
						}

						if ( strategy == TestStrategy.FindFirstError ) {
							testing = false; 
						}
					}

					if ( testing && testData && this[entryIndex].IsFile ) {
						if ( resultHandler != null ) {
							status.SetOperation(TestOperation.EntryData);
							resultHandler(status, null);
						}

                        Crc32 crc = new Crc32();

                        using (Stream entryStream = this.GetInputStream(this[entryIndex]))
                        {

                            byte[] buffer = new byte[4096];
                            long totalBytes = 0;
                            int bytesRead;
                            while ((bytesRead = entryStream.Read(buffer, 0, buffer.Length)) > 0)
                            {
                                crc.Update(buffer, 0, bytesRead);

                                if (resultHandler != null)
                                {
                                    totalBytes += bytesRead;
                                    status.SetBytesTested(totalBytes);
                                    resultHandler(status, null);
                                }
                            }
                        }

						if (this[entryIndex].Crc != crc.Value) {
							status.AddError();
							
							if ( resultHandler != null ) {
								resultHandler(status, "CRC mismatch");
							}

							if ( strategy == TestStrategy.FindFirstError ) {
								testing = false;
							}
						}

						if (( this[entryIndex].Flags & (int)GeneralBitFlags.Descriptor) != 0 ) {
							ZipHelperStream helper = new ZipHelperStream(baseStream_);
							DescriptorData data = new DescriptorData();
							helper.ReadDataDescriptor(this[entryIndex].LocalHeaderRequiresZip64, data);
							if (this[entryIndex].Crc != data.Crc) {
								status.AddError();
							}

							if (this[entryIndex].CompressedSize != data.CompressedSize) {
								status.AddError();
							}

							if (this[entryIndex].Size != data.Size) {
								status.AddError();
							}
						}
					}

					if ( resultHandler != null ) {
						status.SetOperation(TestOperation.EntryComplete);
						resultHandler(status, null);
					}

					entryIndex += 1;
				}

				if ( resultHandler != null ) {
					status.SetOperation(TestOperation.MiscellaneousTests);
					resultHandler(status, null);
				}

				// TODO: the 'Corrina Johns' test where local headers are missing from
				// the central directory.  They are therefore invisible to many archivers.
			}
			catch (Exception ex) {
				status.AddError();

				if ( resultHandler != null ) {
					resultHandler(status, string.Format("Exception during test - '{0}'", ex.Message));
				}
			}

			if ( resultHandler != null ) {
				status.SetOperation(TestOperation.Complete);
				status.SetEntry(null);
				resultHandler(status, null);
			}

			return (status.ErrorCount == 0);
		}
Exemple #26
0
 private void RunUpdates()
 {
     ZipFile file;
     long sizeEntries = 0L;
     long num2 = 0L;
     bool flag = false;
     long destinationPosition = 0L;
     if (this.IsNewArchive)
     {
         file = this;
         file.baseStream_.Position = 0L;
         flag = true;
     }
     else if (this.archiveStorage_.UpdateMode == FileUpdateMode.Direct)
     {
         file = this;
         file.baseStream_.Position = 0L;
         flag = true;
         this.updates_.Sort(new UpdateComparer());
     }
     else
     {
         file = Create(this.archiveStorage_.GetTemporaryOutput());
         file.UseZip64 = this.UseZip64;
         if (this.key != null)
         {
             file.key = (byte[]) this.key.Clone();
         }
     }
     try
     {
         foreach (ZipUpdate update in this.updates_)
         {
             if (update != null)
             {
                 switch (update.Command)
                 {
                     case UpdateCommand.Copy:
                     {
                         if (!flag)
                         {
                             goto Label_00EC;
                         }
                         this.CopyEntryDirect(file, update, ref destinationPosition);
                         continue;
                     }
                     case UpdateCommand.Modify:
                     {
                         this.ModifyEntry(file, update);
                         continue;
                     }
                     case UpdateCommand.Add:
                         goto Label_0104;
                 }
             }
             continue;
         Label_00EC:
             this.CopyEntry(file, update);
             continue;
         Label_0104:
             if (!this.IsNewArchive && flag)
             {
                 file.baseStream_.Position = destinationPosition;
             }
             this.AddEntry(file, update);
             if (flag)
             {
                 destinationPosition = file.baseStream_.Position;
             }
         }
         if (!this.IsNewArchive && flag)
         {
             file.baseStream_.Position = destinationPosition;
         }
         long position = file.baseStream_.Position;
         foreach (ZipUpdate update2 in this.updates_)
         {
             if (update2 != null)
             {
                 sizeEntries += file.WriteCentralDirectoryHeader(update2.OutEntry);
             }
         }
         byte[] comment = (this.newComment_ != null) ? this.newComment_.RawComment : ZipConstants.ConvertToArray(this.comment_);
         using (ZipHelperStream stream = new ZipHelperStream(file.baseStream_))
         {
             stream.WriteEndOfCentralDirectory(this.updateCount_, sizeEntries, position, comment);
         }
         num2 = file.baseStream_.Position;
         foreach (ZipUpdate update3 in this.updates_)
         {
             if (update3 != null)
             {
                 if ((update3.CrcPatchOffset > 0L) && (update3.OutEntry.CompressedSize > 0L))
                 {
                     file.baseStream_.Position = update3.CrcPatchOffset;
                     file.WriteLEInt((int) update3.OutEntry.Crc);
                 }
                 if (update3.SizePatchOffset > 0L)
                 {
                     file.baseStream_.Position = update3.SizePatchOffset;
                     if (update3.OutEntry.LocalHeaderRequiresZip64)
                     {
                         file.WriteLeLong(update3.OutEntry.Size);
                         file.WriteLeLong(update3.OutEntry.CompressedSize);
                     }
                     else
                     {
                         file.WriteLEInt((int) update3.OutEntry.CompressedSize);
                         file.WriteLEInt((int) update3.OutEntry.Size);
                     }
                 }
             }
         }
     }
     catch
     {
         file.Close();
         if (!flag && (file.Name != null))
         {
             File.Delete(file.Name);
         }
         throw;
     }
     if (flag)
     {
         file.baseStream_.SetLength(num2);
         file.baseStream_.Flush();
         this.isNewArchive_ = false;
         this.ReadEntries();
     }
     else
     {
         this.baseStream_.Close();
         this.Reopen(this.archiveStorage_.ConvertTemporaryToFinal());
     }
 }
 public override void Finish()
 {
     if (this.entries != null)
     {
         if (this.curEntry != null)
         {
             this.CloseEntry();
         }
         long count = this.entries.Count;
         long sizeEntries = 0L;
         foreach (ZipEntry entry in this.entries)
         {
             this.WriteLeInt(0x2014b50);
             this.WriteLeShort(0x33);
             this.WriteLeShort(entry.Version);
             this.WriteLeShort(entry.Flags);
             this.WriteLeShort((short) entry.CompressionMethodForHeader);
             this.WriteLeInt((int) entry.DosTime);
             this.WriteLeInt((int) entry.Crc);
             if (entry.IsZip64Forced() || (entry.CompressedSize >= 0xffffffffL))
             {
                 this.WriteLeInt(-1);
             }
             else
             {
                 this.WriteLeInt((int) entry.CompressedSize);
             }
             if (entry.IsZip64Forced() || (entry.Size >= 0xffffffffL))
             {
                 this.WriteLeInt(-1);
             }
             else
             {
                 this.WriteLeInt((int) entry.Size);
             }
             byte[] buffer = ZipConstants.ConvertToArray(entry.Flags, entry.Name);
             if (buffer.Length > 0xffff)
             {
                 throw new ZipException("Name too long.");
             }
             ZipExtraData extraData = new ZipExtraData(entry.ExtraData);
             if (entry.CentralHeaderRequiresZip64)
             {
                 extraData.StartNewEntry();
                 if (entry.IsZip64Forced() || (entry.Size >= 0xffffffffL))
                 {
                     extraData.AddLeLong(entry.Size);
                 }
                 if (entry.IsZip64Forced() || (entry.CompressedSize >= 0xffffffffL))
                 {
                     extraData.AddLeLong(entry.CompressedSize);
                 }
                 if (entry.Offset >= 0xffffffffL)
                 {
                     extraData.AddLeLong(entry.Offset);
                 }
                 extraData.AddNewEntry(1);
             }
             else
             {
                 extraData.Delete(1);
             }
             if (entry.AESKeySize > 0)
             {
                 AddExtraDataAES(entry, extraData);
             }
             byte[] entryData = extraData.GetEntryData();
             byte[] buffer3 = (entry.Comment != null) ? ZipConstants.ConvertToArray(entry.Flags, entry.Comment) : new byte[0];
             if (buffer3.Length > 0xffff)
             {
                 throw new ZipException("Comment too long.");
             }
             this.WriteLeShort(buffer.Length);
             this.WriteLeShort(entryData.Length);
             this.WriteLeShort(buffer3.Length);
             this.WriteLeShort(0);
             this.WriteLeShort(0);
             if (entry.ExternalFileAttributes != -1)
             {
                 this.WriteLeInt(entry.ExternalFileAttributes);
             }
             else if (entry.IsDirectory)
             {
                 this.WriteLeInt(0x10);
             }
             else
             {
                 this.WriteLeInt(0);
             }
             if (entry.Offset >= 0xffffffffL)
             {
                 this.WriteLeInt(-1);
             }
             else
             {
                 this.WriteLeInt((int) entry.Offset);
             }
             if (buffer.Length > 0)
             {
                 base.baseOutputStream_.Write(buffer, 0, buffer.Length);
             }
             if (entryData.Length > 0)
             {
                 base.baseOutputStream_.Write(entryData, 0, entryData.Length);
             }
             if (buffer3.Length > 0)
             {
                 base.baseOutputStream_.Write(buffer3, 0, buffer3.Length);
             }
             sizeEntries += ((0x2e + buffer.Length) + entryData.Length) + buffer3.Length;
         }
         using (ZipHelperStream stream = new ZipHelperStream(base.baseOutputStream_))
         {
             stream.WriteEndOfCentralDirectory(count, sizeEntries, this.offset, this.zipComment);
         }
         this.entries = null;
     }
 }
Exemple #28
0
		/// <summary>
		/// Get the binary data representing this instance.
		/// </summary>
		/// <returns>The raw binary data representing this instance.</returns>
		public byte[] GetData()
		{
			using (MemoryStream ms = new MemoryStream())
			using (ZipHelperStream helperStream = new ZipHelperStream(ms))
			{
				helperStream.IsStreamOwner = false;
				helperStream.WriteByte((byte)flags_);     // Flags
				if ( (flags_ & Flags.ModificationTime) != 0) {
					TimeSpan span = modificationTime_.ToUniversalTime() - new System.DateTime(1970, 1, 1, 0, 0, 0).ToUniversalTime();
					int seconds = (int)span.TotalSeconds;
					helperStream.WriteLEInt(seconds);
				}
				if ( (flags_ & Flags.AccessTime) != 0) {
					TimeSpan span = lastAccessTime_.ToUniversalTime() - new System.DateTime(1970, 1, 1, 0, 0, 0).ToUniversalTime();
					int seconds = (int)span.TotalSeconds;
					helperStream.WriteLEInt(seconds);
				}
				if ( (flags_ & Flags.CreateTime) != 0) {
					TimeSpan span = createTime_.ToUniversalTime() - new System.DateTime(1970, 1, 1, 0, 0, 0).ToUniversalTime();
					int seconds = (int)span.TotalSeconds;
					helperStream.WriteLEInt(seconds);
				}
				return ms.ToArray();
			}
		}
 public override void Finish()
 {
     if (this.entries != null)
     {
         if (this.curEntry != null)
         {
             this.CloseEntry();
         }
         long count       = this.entries.Count;
         long sizeEntries = 0L;
         foreach (ZipEntry entry in this.entries)
         {
             this.WriteLeInt(0x2014b50);
             this.WriteLeShort(0x33);
             this.WriteLeShort(entry.Version);
             this.WriteLeShort(entry.Flags);
             this.WriteLeShort((short)entry.CompressionMethodForHeader);
             this.WriteLeInt((int)entry.DosTime);
             this.WriteLeInt((int)entry.Crc);
             if (entry.IsZip64Forced() || (entry.CompressedSize >= 0xffffffffL))
             {
                 this.WriteLeInt(-1);
             }
             else
             {
                 this.WriteLeInt((int)entry.CompressedSize);
             }
             if (entry.IsZip64Forced() || (entry.Size >= 0xffffffffL))
             {
                 this.WriteLeInt(-1);
             }
             else
             {
                 this.WriteLeInt((int)entry.Size);
             }
             byte[] buffer = ZipConstants.ConvertToArray(entry.Flags, entry.Name);
             if (buffer.Length > 0xffff)
             {
                 throw new ZipException("Name too long.");
             }
             ZipExtraData extraData = new ZipExtraData(entry.ExtraData);
             if (entry.CentralHeaderRequiresZip64)
             {
                 extraData.StartNewEntry();
                 if (entry.IsZip64Forced() || (entry.Size >= 0xffffffffL))
                 {
                     extraData.AddLeLong(entry.Size);
                 }
                 if (entry.IsZip64Forced() || (entry.CompressedSize >= 0xffffffffL))
                 {
                     extraData.AddLeLong(entry.CompressedSize);
                 }
                 if (entry.Offset >= 0xffffffffL)
                 {
                     extraData.AddLeLong(entry.Offset);
                 }
                 extraData.AddNewEntry(1);
             }
             else
             {
                 extraData.Delete(1);
             }
             if (entry.AESKeySize > 0)
             {
                 AddExtraDataAES(entry, extraData);
             }
             byte[] entryData = extraData.GetEntryData();
             byte[] buffer3   = (entry.Comment != null) ? ZipConstants.ConvertToArray(entry.Flags, entry.Comment) : new byte[0];
             if (buffer3.Length > 0xffff)
             {
                 throw new ZipException("Comment too long.");
             }
             this.WriteLeShort(buffer.Length);
             this.WriteLeShort(entryData.Length);
             this.WriteLeShort(buffer3.Length);
             this.WriteLeShort(0);
             this.WriteLeShort(0);
             if (entry.ExternalFileAttributes != -1)
             {
                 this.WriteLeInt(entry.ExternalFileAttributes);
             }
             else if (entry.IsDirectory)
             {
                 this.WriteLeInt(0x10);
             }
             else
             {
                 this.WriteLeInt(0);
             }
             if (entry.Offset >= 0xffffffffL)
             {
                 this.WriteLeInt(-1);
             }
             else
             {
                 this.WriteLeInt((int)entry.Offset);
             }
             if (buffer.Length > 0)
             {
                 base.baseOutputStream_.Write(buffer, 0, buffer.Length);
             }
             if (entryData.Length > 0)
             {
                 base.baseOutputStream_.Write(entryData, 0, entryData.Length);
             }
             if (buffer3.Length > 0)
             {
                 base.baseOutputStream_.Write(buffer3, 0, buffer3.Length);
             }
             sizeEntries += ((0x2e + buffer.Length) + entryData.Length) + buffer3.Length;
         }
         using (ZipHelperStream stream = new ZipHelperStream(base.baseOutputStream_))
         {
             stream.WriteEndOfCentralDirectory(count, sizeEntries, this.offset, this.zipComment);
         }
         this.entries = null;
     }
 }
Exemple #30
0
		/// <summary>
		/// Get the binary data representing this instance.
		/// </summary>
		/// <returns>The raw binary data representing this instance.</returns>
		public byte[] GetData()
		{
			using (MemoryStream ms = new MemoryStream())
			using (ZipHelperStream helperStream = new ZipHelperStream(ms))
			{
				helperStream.IsStreamOwner = false;
				helperStream.WriteLEInt(0);       // Reserved
				helperStream.WriteLEShort(1);     // Tag
				helperStream.WriteLEShort(24);    // Length = 3 x 8.
				helperStream.WriteLELong(lastModificationTime_.ToFileTime());
				helperStream.WriteLELong(lastAccessTime_.ToFileTime());
				helperStream.WriteLELong(createTime_.ToFileTime());
				return ms.ToArray();
			}
		}
        public override void Finish()
        {
            if (this.entries == null)
            {
                return;
            }
            if (this.curEntry != null)
            {
                this.CloseEntry();
            }
            long noOfEntries = (long)this.entries.Count;
            long num         = 0L;

            foreach (ZipEntry zipEntry in this.entries)
            {
                this.WriteLeInt(33639248);
                this.WriteLeShort(51);
                this.WriteLeShort(zipEntry.Version);
                this.WriteLeShort(zipEntry.Flags);
                this.WriteLeShort((int)((short)zipEntry.CompressionMethodForHeader));
                this.WriteLeInt((int)zipEntry.DosTime);
                this.WriteLeInt((int)zipEntry.Crc);
                if (zipEntry.IsZip64Forced() || zipEntry.CompressedSize >= (long)((ulong)-1))
                {
                    this.WriteLeInt(-1);
                }
                else
                {
                    this.WriteLeInt((int)zipEntry.CompressedSize);
                }
                if (zipEntry.IsZip64Forced() || zipEntry.Size >= (long)((ulong)-1))
                {
                    this.WriteLeInt(-1);
                }
                else
                {
                    this.WriteLeInt((int)zipEntry.Size);
                }
                byte[] array = ZipConstants.ConvertToArray(zipEntry.Flags, zipEntry.Name);
                if (array.Length > 65535)
                {
                    throw new ZipException("Name too long.");
                }
                ZipExtraData zipExtraData = new ZipExtraData(zipEntry.ExtraData);
                if (zipEntry.CentralHeaderRequiresZip64)
                {
                    zipExtraData.StartNewEntry();
                    if (zipEntry.IsZip64Forced() || zipEntry.Size >= (long)((ulong)-1))
                    {
                        zipExtraData.AddLeLong(zipEntry.Size);
                    }
                    if (zipEntry.IsZip64Forced() || zipEntry.CompressedSize >= (long)((ulong)-1))
                    {
                        zipExtraData.AddLeLong(zipEntry.CompressedSize);
                    }
                    if (zipEntry.Offset >= (long)((ulong)-1))
                    {
                        zipExtraData.AddLeLong(zipEntry.Offset);
                    }
                    zipExtraData.AddNewEntry(1);
                }
                else
                {
                    zipExtraData.Delete(1);
                }
                if (zipEntry.AESKeySize > 0)
                {
                    ZipOutputStream.AddExtraDataAES(zipEntry, zipExtraData);
                }
                byte[] entryData = zipExtraData.GetEntryData();
                byte[] array2    = (zipEntry.Comment == null) ? new byte[0] : ZipConstants.ConvertToArray(zipEntry.Flags, zipEntry.Comment);
                if (array2.Length > 65535)
                {
                    throw new ZipException("Comment too long.");
                }
                this.WriteLeShort(array.Length);
                this.WriteLeShort(entryData.Length);
                this.WriteLeShort(array2.Length);
                this.WriteLeShort(0);
                this.WriteLeShort(0);
                if (zipEntry.ExternalFileAttributes != -1)
                {
                    this.WriteLeInt(zipEntry.ExternalFileAttributes);
                }
                else if (zipEntry.IsDirectory)
                {
                    this.WriteLeInt(16);
                }
                else
                {
                    this.WriteLeInt(0);
                }
                if (zipEntry.Offset >= (long)((ulong)-1))
                {
                    this.WriteLeInt(-1);
                }
                else
                {
                    this.WriteLeInt((int)zipEntry.Offset);
                }
                if (array.Length > 0)
                {
                    this.baseOutputStream_.Write(array, 0, array.Length);
                }
                if (entryData.Length > 0)
                {
                    this.baseOutputStream_.Write(entryData, 0, entryData.Length);
                }
                if (array2.Length > 0)
                {
                    this.baseOutputStream_.Write(array2, 0, array2.Length);
                }
                num += (long)(46 + array.Length + entryData.Length + array2.Length);
            }
            using (ZipHelperStream zipHelperStream = new ZipHelperStream(this.baseOutputStream_))
            {
                zipHelperStream.WriteEndOfCentralDirectory(noOfEntries, num, this.offset, this.zipComment);
            }
            this.entries = null;
        }
Exemple #32
0
		/// <summary>
		/// Commit current updates, updating this archive.
		/// </summary>
		/// <seealso cref="BeginUpdate()"></seealso>
		/// <seealso cref="AbortUpdate"></seealso>
		/// <exception cref="ObjectDisposedException">ZipFile has been closed.</exception>
		public void CommitUpdate()
		{
			if ( isDisposed_ ) {
				throw new ObjectDisposedException("ZipFile");
			}
			
			CheckUpdating();

			try {
				updateIndex_.Clear();
				updateIndex_=null;

				if( contentsEdited_ ) {
					RunUpdates();
				}
				else if( commentEdited_ ) {
					UpdateCommentOnly();
				}
				else {
					// Create an empty archive if none existed originally.
					if( entries_.Length==0 ) {
						byte[] theComment=(newComment_!=null)?newComment_.RawComment:ZipConstants.ConvertToArray(comment_);
						using( ZipHelperStream zhs=new ZipHelperStream(baseStream_) ) {
							zhs.WriteEndOfCentralDirectory(0, 0, 0, theComment);
						}
					}
				}

			}
			finally {
				PostUpdateCleanup();
			}
		}
        /// <summary>
        /// Finishes the stream.  This will write the central directory at the
        /// end of the zip file and flush the stream.
        /// </summary>
        /// <remarks>
        /// This is automatically called when the stream is closed.
        /// </remarks>
        /// <exception cref="System.IO.IOException">
        /// An I/O error occurs.
        /// </exception>
        /// <exception cref="ZipException">
        /// Comment exceeds the maximum length<br/>
        /// Entry name exceeds the maximum length
        /// </exception>
        public override void Finish()
        {
            if (entries == null)
            {
                return;
            }

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

            long numEntries = entries.Count;
            long sizeEntries = 0;

            foreach (ZipEntry entry in entries)
            {
                WriteLeInt(ZipConstants.CentralHeaderSignature);
                WriteLeShort(ZipConstants.VersionMadeBy);
                WriteLeShort(entry.Version);
                WriteLeShort(entry.Flags);
                WriteLeShort((short) entry.CompressionMethodForHeader);
                WriteLeInt((int) entry.DosTime);
                WriteLeInt((int) entry.Crc);

                if (entry.IsZip64Forced() ||
                    (entry.CompressedSize >= uint.MaxValue))
                {
                    WriteLeInt(-1);
                }
                else
                {
                    WriteLeInt((int) entry.CompressedSize);
                }

                if (entry.IsZip64Forced() ||
                    (entry.Size >= uint.MaxValue))
                {
                    WriteLeInt(-1);
                }
                else
                {
                    WriteLeInt((int) entry.Size);
                }

                byte[] name = ZipConstants.ConvertToArray(entry.Flags, entry.Name);

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

                var ed = new ZipExtraData(entry.ExtraData);

                if (entry.CentralHeaderRequiresZip64)
                {
                    ed.StartNewEntry();
                    if (entry.IsZip64Forced() ||
                        (entry.Size >= 0xffffffff))
                    {
                        ed.AddLeLong(entry.Size);
                    }

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

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

                    ed.AddNewEntry(1);
                }
                else
                {
                    ed.Delete(1);
                }

#if !NET_1_1 && !NETCF_2_0
                if (entry.AESKeySize > 0)
                {
                    AddExtraDataAES(entry, ed);
                }
#endif
                byte[] extra = ed.GetEntryData();

                byte[] entryComment =
                    (entry.Comment != null)
                        ? ZipConstants.ConvertToArray(entry.Flags, entry.Comment)
                        : new byte[0];

                if (entryComment.Length > 0xffff)
                {
                    throw new ZipException("Comment too long.");
                }

                WriteLeShort(name.Length);
                WriteLeShort(extra.Length);
                WriteLeShort(entryComment.Length);
                WriteLeShort(0); // disk number
                WriteLeShort(0); // internal file attributes
                // external file attributes

                if (entry.ExternalFileAttributes != -1)
                {
                    WriteLeInt(entry.ExternalFileAttributes);
                }
                else
                {
                    if (entry.IsDirectory)
                    {
                        // mark entry as directory (from nikolam.AT.perfectinfo.com)
                        WriteLeInt(16);
                    }
                    else
                    {
                        WriteLeInt(0);
                    }
                }

                if (entry.Offset >= uint.MaxValue)
                {
                    WriteLeInt(-1);
                }
                else
                {
                    WriteLeInt((int) entry.Offset);
                }

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

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

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

                sizeEntries += ZipConstants.CentralHeaderBaseSize + name.Length + extra.Length + entryComment.Length;
            }

            using (var zhs = new ZipHelperStream(baseOutputStream_))
            {
                zhs.WriteEndOfCentralDirectory(numEntries, sizeEntries, offset, zipComment);
            }

            entries = null;
        }
Exemple #34
0
		void UpdateCommentOnly()
		{
			long baseLength = baseStream_.Length;

			ZipHelperStream updateFile = null;

			if ( archiveStorage_.UpdateMode == FileUpdateMode.Safe ) {
				Stream copyStream = archiveStorage_.MakeTemporaryCopy(baseStream_);
				updateFile = new ZipHelperStream(copyStream);
				updateFile.IsStreamOwner = true;

				baseStream_.Close();
				baseStream_ = null;
			}
			else {
				if (archiveStorage_.UpdateMode == FileUpdateMode.Direct) {
					// TODO: archiveStorage wasnt originally intended for this use.
					// Need to revisit this to tidy up handling as archive storage currently doesnt 
					// handle the original stream well.
					// The problem is when using an existing zip archive with an in memory archive storage.
					// The open stream wont support writing but the memory storage should open the same file not an in memory one.

					// Need to tidy up the archive storage interface and contract basically.
					baseStream_ = archiveStorage_.OpenForDirectUpdate(baseStream_);
					updateFile = new ZipHelperStream(baseStream_);
				}
				else {
					baseStream_.Close();
					baseStream_ = null;
					updateFile = new ZipHelperStream(Name);
				}
			}

			using ( updateFile ) {
				long locatedCentralDirOffset = 
					updateFile.LocateBlockWithSignature(ZipConstants.EndOfCentralDirectorySignature, 
														baseLength, ZipConstants.EndOfCentralRecordBaseSize, 0xffff);
				if ( locatedCentralDirOffset < 0 ) {
					throw new ZipException("Cannot find central directory");
				}

				const int CentralHeaderCommentSizeOffset = 16;
				updateFile.Position += CentralHeaderCommentSizeOffset;

				byte[] rawComment = newComment_.RawComment;

				updateFile.WriteLEShort(rawComment.Length);
				updateFile.Write(rawComment, 0, rawComment.Length);
				updateFile.SetLength(updateFile.Position);
			}

			if ( archiveStorage_.UpdateMode == FileUpdateMode.Safe ) {
				Reopen(archiveStorage_.ConvertTemporaryToFinal());
			}
			else {
				ReadEntries();
			}
		}
Exemple #35
0
 public void CommitUpdate()
 {
     if (this.isDisposed_)
     {
         throw new ObjectDisposedException("ZipFile");
     }
     this.CheckUpdating();
     try
     {
         this.updateIndex_.Clear();
         this.updateIndex_ = null;
         if (this.contentsEdited_)
         {
             this.RunUpdates();
         }
         else if (this.commentEdited_)
         {
             this.UpdateCommentOnly();
         }
         else if (this.entries_.Length == 0)
         {
             byte[] comment = (this.newComment_ != null) ? this.newComment_.RawComment : ZipConstants.ConvertToArray(this.comment_);
             using (ZipHelperStream stream = new ZipHelperStream(this.baseStream_))
             {
                 stream.WriteEndOfCentralDirectory(0L, 0L, 0L, comment);
             }
         }
     }
     finally
     {
         this.PostUpdateCleanup();
     }
 }
Exemple #36
0
		// NOTE this returns the offset of the first byte after the signature.
		long LocateBlockWithSignature(int signature, long endLocation, int minimumBlockSize, int maximumVariableData)
		{
			using ( ZipHelperStream les = new ZipHelperStream(baseStream_) ) {
				return les.LocateBlockWithSignature(signature, endLocation, minimumBlockSize, maximumVariableData);
			}
		}
Exemple #37
0
        /// <summary>
        /// Commit current updates, updating this archive.
        /// </summary>
        /// <seealso cref="BeginUpdate()"></seealso>
        /// <seealso cref="AbortUpdate"></seealso>
        public void CommitUpdate()
        {
            CheckUpdating();

            if ( contentsEdited_ ) {
                RunUpdates();
            }
            else if ( commentEdited_ ) {
                UpdateCommentOnly();
            }
            else {
                // Create an empty archive if none existed originally.
                if ( (entries_ != null) && (entries_.Length == 0) ) {
                    byte[] theComment = (newComment_ != null) ? newComment_.RawComment : ZipConstants.ConvertToArray(comment_);
                    using ( ZipHelperStream zhs = new ZipHelperStream(baseStream_) ) {
                        zhs.WriteEndOfCentralDirectory(0, 0, 0, theComment);
                    }
                }
            }

            PostUpdateCleanup();
        }
        /// <summary>
        /// Finishes the stream.  This will write the central directory at the
        /// end of the zip file and flush the stream.
        /// </summary>
        /// <remarks>
        /// This is automatically called when the stream is closed.
        /// </remarks>
        /// <exception cref="System.IO.IOException">
        /// An I/O error occurs.
        /// </exception>
        /// <exception cref="ZipException">
        /// Comment exceeds the maximum length<br/>
        /// Entry name exceeds the maximum length
        /// </exception>
        public override void Finish()
        {
            if (entries == null)
            {
                return;
            }

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

            long numEntries  = entries.Count;
            long sizeEntries = 0;

            foreach (ZipEntryM entry in entries)
            {
                WriteLeInt(ZipConstants.CentralHeaderSignature);
                WriteLeShort(ZipConstants.VersionMadeBy);
                WriteLeShort(entry.Version);
                WriteLeShort(entry.Flags);
                WriteLeShort((short)entry.CompressionMethodForHeader);
                WriteLeInt((int)entry.DosTime);
                WriteLeInt((int)entry.Crc);

                if (entry.IsZip64Forced() ||
                    (entry.CompressedSize >= uint.MaxValue))
                {
                    WriteLeInt(-1);
                }
                else
                {
                    WriteLeInt((int)entry.CompressedSize);
                }

                if (entry.IsZip64Forced() ||
                    (entry.Size >= uint.MaxValue))
                {
                    WriteLeInt(-1);
                }
                else
                {
                    WriteLeInt((int)entry.Size);
                }

                byte[] name = ZipConstants.ConvertToArray(entry.Flags, entry.Name);

                if (name.Length > 0xffff)
                {
                    //throw new ZipException("Name too long.");
                }

                var ed = new ZipExtraData(entry.ExtraData);

                if (entry.CentralHeaderRequiresZip64)
                {
                    ed.StartNewEntry();
                    if (entry.IsZip64Forced() ||
                        (entry.Size >= 0xffffffff))
                    {
                        ed.AddLeLong(entry.Size);
                    }

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

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

                    ed.AddNewEntry(1);
                }
                else
                {
                    ed.Delete(1);
                }

                if (entry.AESKeySize > 0)
                {
                    AddExtraDataAES(entry, ed);
                }
                byte[] extra = ed.GetEntryData();

                byte[] entryComment =
                    (entry.Comment != null) ?
                    ZipConstants.ConvertToArray(entry.Flags, entry.Comment) :
                    new byte[0];

                if (entryComment.Length > 0xffff)
                {
                    //throw new ZipException("Comment too long.");
                }

                WriteLeShort(name.Length);
                WriteLeShort(extra.Length);
                WriteLeShort(entryComment.Length);
                WriteLeShort(0);                    // disk number
                WriteLeShort(0);                    // internal file attributes
                // external file attributes

                if (entry.ExternalFileAttributes != -1)
                {
                    WriteLeInt(entry.ExternalFileAttributes);
                }
                else
                {
                    if (entry.IsDirectory)                                               // mark entry as directory (from nikolam.AT.perfectinfo.com)
                    {
                        WriteLeInt(16);
                    }
                    else
                    {
                        WriteLeInt(0);
                    }
                }

                if (entry.Offset >= uint.MaxValue)
                {
                    WriteLeInt(-1);
                }
                else
                {
                    WriteLeInt((int)entry.Offset);
                }

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

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

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

                sizeEntries += ZipConstants.CentralHeaderBaseSize + name.Length + extra.Length + entryComment.Length;
            }

            using (ZipHelperStream zhs = new ZipHelperStream(baseOutputStream_)) {
                zhs.WriteEndOfCentralDirectory(numEntries, sizeEntries, offset, zipComment);
            }

            entries = null;
        }
Exemple #39
0
 /// <summary>
 /// Get the binary data representing this instance.
 /// </summary>
 /// <returns>The raw binary data representing this instance.</returns>
 public byte[] GetData() {
     using (var ms=new MemoryStream())
     using (var helperStream=new ZipHelperStream(ms)) {
         helperStream.IsStreamOwner=false;
         helperStream.WriteByte((byte)_flags); // Flags
         if ((_flags&Flags.ModificationTime)!=0) {
             TimeSpan span=_modificationTime.ToUniversalTime()-
                             new DateTime(1970, 1, 1, 0, 0, 0).ToUniversalTime();
             var seconds=(int)span.TotalSeconds;
             helperStream.WriteLEInt(seconds);
         }
         if ((_flags&Flags.AccessTime)!=0) {
             TimeSpan span=_lastAccessTime.ToUniversalTime()-
                             new DateTime(1970, 1, 1, 0, 0, 0).ToUniversalTime();
             var seconds=(int)span.TotalSeconds;
             helperStream.WriteLEInt(seconds);
         }
         if ((_flags&Flags.CreateTime)!=0) {
             TimeSpan span=_createTime.ToUniversalTime()-new DateTime(1970, 1, 1, 0, 0, 0).ToUniversalTime();
             var seconds=(int)span.TotalSeconds;
             helperStream.WriteLEInt(seconds);
         }
         return ms.ToArray();
     }
 }