Example #1
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)
        {
            ZipEntry result = new ZipEntry(nameTransform_.TransformDirectory(directoryName));

            result.IsUnicodeText = isUnicodeText_;
            result.Size          = 0;

            int externalAttributes = 0;

            DirectoryInfo di = null;

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


            if ((di != null) && di.Exists)
            {
                switch (timeSetting_)
                {
                case TimeSetting.CreateTime:
                    result.DateTime = di.CreationTime;
                    break;

                case TimeSetting.CreateTimeUtc:
#if NETCF_1_0 || NETCF_2_0
                    result.DateTime = di.CreationTime.ToUniversalTime();
#else
                    result.DateTime = di.CreationTimeUtc;
#endif
                    break;

                case TimeSetting.LastAccessTime:
                    result.DateTime = di.LastAccessTime;
                    break;

                case TimeSetting.LastAccessTimeUtc:
#if NETCF_1_0 || NETCF_2_0
                    result.DateTime = di.LastAccessTime.ToUniversalTime();
#else
                    result.DateTime = di.LastAccessTimeUtc;
#endif
                    break;

                case TimeSetting.LastWriteTime:
                    result.DateTime = di.LastWriteTime;
                    break;

                case TimeSetting.LastWriteTimeUtc:
#if NETCF_1_0 || NETCF_2_0
                    result.DateTime = di.LastWriteTime.ToUniversalTime();
#else
                    result.DateTime = di.LastWriteTimeUtc;
#endif
                    break;

                case TimeSetting.Fixed:
                    result.DateTime = fixedDateTime_;
                    break;

                default:
                    throw new ZipException("Unhandled time setting in MakeDirectoryEntry");
                }

                externalAttributes = ((int)di.Attributes & getAttributes_);
            }
            else
            {
                if (timeSetting_ == TimeSetting.Fixed)
                {
                    result.DateTime = fixedDateTime_;
                }
            }

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

            return(result);
        }
Example #2
0
        // Write the local file header
        // TODO: ZipHelperStream.WriteLocalHeader is not yet used and needs checking for ZipFile and ZipOuptutStream usage
        void WriteLocalHeader(ZipEntry entry, EntryPatchData patchData)
        {
            CompressionMethod method = entry.CompressionMethod;
            bool headerInfoAvailable = true;             // How to get this?
            bool patchEntryHeader    = false;

            WriteLEInt(ZipConstants.LocalHeaderSignature);

            WriteLEShort(entry.Version);
            WriteLEShort(entry.Flags);
            WriteLEShort((byte)method);
            WriteLEInt((int)entry.DosTime);

            if (headerInfoAvailable == true)
            {
                WriteLEInt((int)entry.Crc);
                if (entry.LocalHeaderRequiresZip64)
                {
                    WriteLEInt(-1);
                    WriteLEInt(-1);
                }
                else
                {
                    WriteLEInt(entry.IsCrypted ? (int)entry.CompressedSize + ZipConstants.CryptoHeaderSize : (int)entry.CompressedSize);
                    WriteLEInt((int)entry.Size);
                }
            }
            else
            {
                if (patchData != null)
                {
                    patchData.CrcPatchOffset = stream_.Position;
                }
                WriteLEInt(0);                  // Crc

                if (patchData != null)
                {
                    patchData.SizePatchOffset = stream_.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
                }
            }

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

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

            ZipExtraData ed = new ZipExtraData(entry.ExtraData);

            if (entry.LocalHeaderRequiresZip64 && (headerInfoAvailable || patchEntryHeader))
            {
                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 (patchData != null)
                {
                    patchData.SizePatchOffset = ed.CurrentReadIndex;
                }
            }
            else
            {
                ed.Delete(1);
            }

            byte[] extra = ed.GetEntryData();

            WriteLEShort(name.Length);
            WriteLEShort(extra.Length);

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

            if (entry.LocalHeaderRequiresZip64 && patchEntryHeader)
            {
                patchData.SizePatchOffset += stream_.Position;
            }

            if (extra.Length > 0)
            {
                stream_.Write(extra, 0, extra.Length);
            }
        }
Example #3
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="entryName">An alternative name to be used for the new entry. Null if not applicable.</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, string entryName, bool useFileSystem)
        {
            ZipEntry result = new ZipEntry(nameTransform_.TransformFile(entryName != null && entryName.Length > 0 ? entryName : fileName));

            result.IsUnicodeText = isUnicodeText_;

            int  externalAttributes = 0;
            bool useAttributes      = (setAttributes_ != 0);

            FileInfo fi = null;

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

            if ((fi != null) && fi.Exists)
            {
                switch (timeSetting_)
                {
                case TimeSetting.CreateTime:
                    result.DateTime = fi.CreationTime;
                    break;

                case TimeSetting.CreateTimeUtc:
#if NETCF_1_0 || NETCF_2_0
                    result.DateTime = fi.CreationTime.ToUniversalTime();
#else
                    result.DateTime = fi.CreationTimeUtc;
#endif
                    break;

                case TimeSetting.LastAccessTime:
                    result.DateTime = fi.LastAccessTime;
                    break;

                case TimeSetting.LastAccessTimeUtc:
#if NETCF_1_0 || NETCF_2_0
                    result.DateTime = fi.LastAccessTime.ToUniversalTime();
#else
                    result.DateTime = fi.LastAccessTimeUtc;
#endif
                    break;

                case TimeSetting.LastWriteTime:
                    result.DateTime = fi.LastWriteTime;
                    break;

                case TimeSetting.LastWriteTimeUtc:
#if NETCF_1_0 || NETCF_2_0
                    result.DateTime = fi.LastWriteTime.ToUniversalTime();
#else
                    result.DateTime = fi.LastWriteTimeUtc;
#endif
                    break;

                case TimeSetting.Fixed:
                    result.DateTime = fixedDateTime_;
                    break;

                default:
                    throw new ZipException("Unhandled time setting in MakeFileEntry");
                }

                result.Size = fi.Length;

                useAttributes      = true;
                externalAttributes = ((int)fi.Attributes & getAttributes_);
            }
            else
            {
                if (timeSetting_ == TimeSetting.Fixed)
                {
                    result.DateTime = fixedDateTime_;
                }
            }

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

            return(result);
        }