示例#1
0
文件: Zip.cs 项目: zixing131/LAEACC
        /// <summary>
        ///
        /// </summary>
        /// <param name="Stream"></param>
        public void SaveToStream(Stream Stream)
        {
            ZipLocalFile        ZipFile;
            ZipCentralDirectory ZipDir;
            ZipFileHeader       ZipFileHeader;
            string FileName;
            long   CentralStartPos, CentralEndPos;

            for (int i = 0; i < FFileList.Count; i++)
            {
                ZipFile          = new ZipLocalFile();
                ZipFile.FileData = new MemoryStream();
                FileName         = FFileList[i].Replace(FRootFolder + "\\", "");
                ZipFile.LocalFileHeader.FileName = FileName;
                using (FileStream file = new FileStream(FFileList[i], FileMode.Open))
                {
                    if (file.Length > 128)
                    {
                        using (DeflateStream deflate = new DeflateStream(ZipFile.FileData, CompressionMode.Compress, true))
                            ZipFile.LocalFileHeader.Crc32 = CopyStream(file, deflate);
                        ZipFile.LocalFileHeader.CompressionMethod = 8;
                    }
                    else
                    {
                        ZipFile.LocalFileHeader.Crc32             = CopyStream(file, ZipFile.FileData);
                        ZipFile.LocalFileHeader.CompressionMethod = 0;
                    }
                    ZipFile.LocalFileHeader.CompressedSize   = (uint)ZipFile.FileData.Length;
                    ZipFile.LocalFileHeader.UnCompressedSize = (uint)file.Length;
                }
                ZipFile.Offset = (uint)Stream.Position;
                ZipFile.LocalFileHeader.LastModFileDate = GetDosDateTime(File.GetLastWriteTime(FFileList[i]));
                ZipFile.SaveToStream(Stream);
                ZipFile.FileData = null;
                FFileObjects.Add(ZipFile);
            }

            CentralStartPos = Stream.Position;
            for (int i = 0; i < FFileObjects.Count; i++)
            {
                ZipFile       = FFileObjects[i];
                ZipFileHeader = new ZipFileHeader();
                ZipFileHeader.CompressionMethod         = ZipFile.LocalFileHeader.CompressionMethod;
                ZipFileHeader.LastModFileDate           = ZipFile.LocalFileHeader.LastModFileDate;
                ZipFileHeader.GeneralPurpose            = ZipFile.LocalFileHeader.GeneralPurpose;
                ZipFileHeader.Crc32                     = ZipFile.LocalFileHeader.Crc32;
                ZipFileHeader.CompressedSize            = ZipFile.LocalFileHeader.CompressedSize;
                ZipFileHeader.UnCompressedSize          = ZipFile.LocalFileHeader.UnCompressedSize;
                ZipFileHeader.RelativeOffsetLocalHeader = ZipFile.Offset;
                ZipFileHeader.FileName                  = ZipFile.LocalFileHeader.FileName;
                ZipFileHeader.SaveToStream(Stream);
            }
            CentralEndPos = Stream.Position;
            ZipDir        = new ZipCentralDirectory();
            ZipDir.TotalOfEntriesCentralDirOnDisk = (ushort)FFileList.Count;
            ZipDir.TotalOfEntriesCentralDir       = (ushort)FFileList.Count;
            ZipDir.SizeOfCentralDir      = (uint)(CentralEndPos - CentralStartPos);
            ZipDir.OffsetStartingDiskDir = (uint)CentralStartPos;
            ZipDir.SaveToStream(Stream);
        }
示例#2
0
        /// <summary>
        /// Creates the zip and writes it to rhe Stream
        /// </summary>
        /// <param name="Stream"></param>
        public void SaveToStream(Stream Stream)
        {
            ZipLocalFile        ZipFile;
            ZipCentralDirectory ZipDir;
            ZipFileHeader       ZipFileHeader;
            long CentralStartPos, CentralEndPos;

            for (int i = 0; i < fileList.Count; i++)
            {
                ZipFile = new ZipLocalFile();
                using (ZipFile.FileData = new MemoryStream())
                {
                    if (fileList[i].Disk)
                    {
                        ZipFile.LocalFileHeader.FileName = fileList[i].Name.Replace(rootFolder + Path.DirectorySeparatorChar, "");
                        using (FileStream file = new FileStream(fileList[i].Name, FileMode.Open))
                            AddStreamToZip(file, ZipFile);
                    }
                    else
                    {
                        ZipFile.LocalFileHeader.FileName = fileList[i].Name;
                        fileList[i].Stream.Position      = 0;
                        AddStreamToZip(fileList[i].Stream, ZipFile);
                    }
                    ZipFile.Offset = (uint)Stream.Position;
                    ZipFile.LocalFileHeader.LastModFileDate = fileList[i].FileDateTime;
                    ZipFile.SaveToStream(Stream);
                }
                ZipFile.FileData = null;
                fileObjects.Add(ZipFile);
            }

            CentralStartPos = Stream.Position;
            for (int i = 0; i < fileObjects.Count; i++)
            {
                ZipFile       = fileObjects[i];
                ZipFileHeader = new ZipFileHeader();
                ZipFileHeader.CompressionMethod         = ZipFile.LocalFileHeader.CompressionMethod;
                ZipFileHeader.LastModFileDate           = ZipFile.LocalFileHeader.LastModFileDate;
                ZipFileHeader.GeneralPurpose            = ZipFile.LocalFileHeader.GeneralPurpose;
                ZipFileHeader.Crc32                     = ZipFile.LocalFileHeader.Crc32;
                ZipFileHeader.CompressedSize            = ZipFile.LocalFileHeader.CompressedSize;
                ZipFileHeader.UnCompressedSize          = ZipFile.LocalFileHeader.UnCompressedSize;
                ZipFileHeader.RelativeOffsetLocalHeader = ZipFile.Offset;
                ZipFileHeader.FileName                  = ZipFile.LocalFileHeader.FileName;
                ZipFileHeader.SaveToStream(Stream);
            }
            CentralEndPos = Stream.Position;
            ZipDir        = new ZipCentralDirectory();
            ZipDir.TotalOfEntriesCentralDirOnDisk = (ushort)fileList.Count;
            ZipDir.TotalOfEntriesCentralDir       = (ushort)fileList.Count;
            ZipDir.SizeOfCentralDir      = (uint)(CentralEndPos - CentralStartPos);
            ZipDir.OffsetStartingDiskDir = (uint)CentralStartPos;
            ZipDir.SaveToStream(Stream);
        }