Ejemplo n.º 1
0
        /// <summary>
        /// Dump debug information to console
        /// </summary>
        //public void Dump()
        //{
        // TODO
        //}
        #endregion Public Methods

        #region Private Constructors
        /// <summary>
        /// Defaut empty constructor
        /// </summary>
        private SevenZipArchive()
        {
            stream     = null;
            fileAccess = null;
            Header     = null;
            IsValid    = false;
        }
Ejemplo 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;
        }
Ejemplo n.º 3
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;
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Close current archive and stream. Archive is committed after this
 /// </summary>
 public void Close()
 {
     signatureHeader = new SignatureHeader();
     if (stream != null)
     {
         stream.Close();
         stream.Dispose();
         stream = null;
     }
     fileAccess = null;
     Header     = null;
     IsValid    = false;
 }
Ejemplo n.º 5
0
        /// <summary>
        /// Create a new 7zip file for writing
        /// </summary>
        private void create()
        {
            signatureHeader = new SignatureHeader()
            {
                Signature      = kSignature.ToArray(),
                ArchiveVersion = new ArchiveVersion()
                {
                    Major = 0,
                    Minor = 2,
                },
            };
            stream.Write(signatureHeader.GetByteArray(), 0, Marshal.SizeOf(signatureHeader));

            Header = new SevenZipHeader(null, true);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Open an existing 7zip file for reading
        /// </summary>
        private void open()
        {
            SignatureHeader sig = stream.ReadStruct <SignatureHeader>();

            if (!sig.Signature.SequenceEqual(kSignature))
            {
                throw new SevenZipException("File is not a valid 7zip file.");
            }
            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 = CRC.Calculate(sig.StartHeader.GetByteArray());
                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 = CRC.Calculate(buffer);
                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, null);

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

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