Esempio n. 1
0
        internal SevenZipCompressor(Stream stream, SevenZipHeader header)
        {
            this.stream = stream;
            this.header = header;

            if (stream == null || !stream.CanWrite)
            {
                throw new ArgumentNullException("Stream isn't suitable for compression.");
            }

            if (header == null)
            {
                throw new ArgumentNullException("Header has not been prepared properly.");
            }

            // init file list
            _Files = new List <SevenZipArchiveFile>();
            Files  = _Files;

            // default values
            ProgressDelegate           = null;
            CompressHeader             = true;
            PreserveDirectoryStructure = true;
            Solid = true;
        }
Esempio n. 2
0
        internal SevenZipExtractor(Stream stream, SevenZipHeader header)
        {
            this.stream = stream;
            this.header = header;

            if (stream == null || !stream.CanRead || stream.Length == 0)
            {
                throw new ArgumentNullException("Stream isn't suitable for extraction.");
            }

            if (header == null || header.RawHeader == null)
            {
                throw new ArgumentNullException("Header has not been parsed and/or decompressed properly.");
            }

            // init file lists
            buildFilesIndex();

            // default values
            OverwriteDelegate      = null;
            ProgressDelegate       = null;
            AllowFileDeletions     = false;
            OverwriteExistingFiles = false;
            Password = null;
            PreserveDirectoryStructure = true;
            SkipExistingFiles          = false;
        }
Esempio n. 3
0
 /// <summary>
 /// Defaut constructor
 /// </summary>
 public SevenZipArchive()
 {
     stream     = null;
     fileAccess = null;
     Header     = null;
     IsValid    = false;
 }
Esempio n. 4
0
 /// <summary>
 /// Close current archive and stream. Archive is committed after this
 /// </summary>
 public void Close()
 {
     this.signatureHeader = new SignatureHeader();
     if (this.stream != null)
     {
         this.stream.Close();
         this.stream.Dispose();
         this.stream = null;
     }
     this.fileAccess = null;
     this.Header     = null;
     this.IsValid    = false;
 }
Esempio n. 5
0
        /// <summary>
        /// Create a new 7zip file for writing
        /// </summary>
        void Create()
        {
            this.signatureHeader = new SignatureHeader()
            {
                Signature      = kSignature.ToArray(),
                ArchiveVersion = new ArchiveVersion()
                {
                    Major = 0,
                    Minor = 2,
                },
            };
            stream.Write(this.signatureHeader.GetByteArray(), 0, Marshal.SizeOf(this.signatureHeader));

            this.Header = new SevenZipHeader(null, true);
        }
Esempio n. 6
0
        public SevenZipExtractor(Stream stream, SevenZipHeader header)
        {
            this.stream = stream;
            this.header = header;

            if (stream == null || !stream.CanRead || stream.Length == 0)
            {
                throw new ArgumentNullException("Stream isn't suitable for extraction.");
            }

            if (header == null || header.RawHeader == null)
            {
                throw new ArgumentNullException("Header has not been parsed and/or decompressed properly.");
            }

            buildFilesIndex();
        }
Esempio n. 7
0
        public SevenZipCompressor(Stream stream, SevenZipHeader header)
        {
            this.stream = stream;
            this.header = header;

            if (stream == null || !stream.CanWrite)
            {
                throw new ArgumentNullException("Stream isn't suitable for extraction.");
            }

            if (header == null)
            {
                throw new ArgumentNullException("Header has not been prepared properly.");
            }

            _Files         = new List <SevenZipArchiveFile>();
            Files          = new ReadOnlyCollection <SevenZipArchiveFile>(_Files);
            Solid          = true;
            CompressHeader = true;
        }
Esempio n. 8
0
        /// <summary>
        /// Open an existing 7zip file for reading
        /// </summary>
        void Open()
        {
            SignatureHeader sig = stream.ReadStruct <SignatureHeader>();

            if (!sig.Signature.SequenceEqual(kSignature))
            {
                throw new SevenZipException("File is not a valid 7zip file.");
            }
            this.signatureHeader = sig;

            // some debug info

            Trace.TraceInformation("Opening 7zip file:");
            Trace.Indent();

            try
            {
                Trace.TraceInformation($"Version: {sig.ArchiveVersion.Major}.{sig.ArchiveVersion.Minor}");
                Trace.TraceInformation($"StartHeaderCRC: {sig.StartHeaderCRC.ToString("X8")}");
                Trace.TraceInformation($"NextHeaderOffset: {sig.StartHeader.NextHeaderOffset}");
                Trace.TraceInformation($"NextHeaderCRC: {sig.StartHeader.NextHeaderCRC.ToString("X8")}");
                Trace.TraceInformation($"NextHeaderSize: {sig.StartHeader.NextHeaderSize}");
                Trace.TraceInformation($"All headers: " + (sig.StartHeader.NextHeaderSize + (uint)Marshal.SizeOf(sig)) + " bytes");

                {
                    uint crc32 = new CRC().Calculate(sig.StartHeader.GetByteArray()).Result;
                    if (crc32 != sig.StartHeaderCRC)
                    {
                        throw new SevenZipException("StartHeaderCRC mismatch: " + crc32.ToString("X8"));
                    }
                }

                // buffer header in memory for further processing

                byte[] buffer = new byte[sig.StartHeader.NextHeaderSize];
                stream.Seek((long)sig.StartHeader.NextHeaderOffset, SeekOrigin.Current);
                if (stream.Read(buffer, 0, (int)sig.StartHeader.NextHeaderSize) != (int)sig.StartHeader.NextHeaderSize)
                {
                    throw new SevenZipException("Reached end of file before end of header.");
                }

                {
                    uint crc32 = new CRC().Calculate(buffer).Result;
                    if (crc32 != sig.StartHeader.NextHeaderCRC)
                    {
                        throw new SevenZipException("StartHeader.NextHeaderCRC mismatch: " + crc32.ToString("X8"));
                    }
                }

                // initiate header parsing

                Trace.TraceInformation("Parsing 7zip file header");
                Header = new SevenZipHeader(new MemoryStream(buffer));
                Header.Parse();

                // decompress encoded header if found

                if (Header.RawHeader == null && Header.EncodedHeader != null)
                {
                    Trace.TraceInformation("Encoded header detected, decompressing.");
                    Stream newHeaderStream = new MemoryStream();
                    (new SevenZipStreamsExtractor(stream, Header.EncodedHeader)).Extract(0, newHeaderStream);

                    Trace.TraceInformation("Parsing decompressed header.");
                    newHeaderStream.Position = 0;
                    SevenZipHeader
                        newHeader = new SevenZipHeader(newHeaderStream);
                    newHeader.Parse();
                    Header = newHeader;
                }

                IsValid = true;
            }
            finally
            {
                Trace.Unindent();
                Trace.TraceInformation("Done parsing 7zip file header.");
            }
        }