Пример #1
0
        public CabUnpacker(CabEngine cabEngine)
            : base(cabEngine)
        {
            this.fdiAllocMemHandler    = this.CabAllocMem;
            this.fdiFreeMemHandler     = this.CabFreeMem;
            this.fdiOpenStreamHandler  = this.CabOpenStream;
            this.fdiReadStreamHandler  = this.CabReadStream;
            this.fdiWriteStreamHandler = this.CabWriteStream;
            this.fdiCloseStreamHandler = this.CabCloseStream;
            this.fdiSeekStreamHandler  = this.CabSeekStream;

            this.fdiHandle = NativeMethods.FDI.Create(
                this.fdiAllocMemHandler,
                this.fdiFreeMemHandler,
                this.fdiOpenStreamHandler,
                this.fdiReadStreamHandler,
                this.fdiWriteStreamHandler,
                this.fdiCloseStreamHandler,
                this.fdiSeekStreamHandler,
                NativeMethods.FDI.CPU_80386,
                this.ErfHandle.AddrOfPinnedObject());
            if (this.Erf.Error)
            {
                int error     = this.Erf.Oper;
                int errorCode = this.Erf.Type;
                this.ErfHandle.Free();
                throw new CabException(
                          error,
                          errorCode,
                          CabException.GetErrorMessage(error, errorCode, true));
            }
        }
Пример #2
0
        private bool IsCabinet(Stream cabStream, out short id, out int cabFolderCount, out int fileCount)
        {
            int streamHandle = this.StreamHandles.AllocHandle(cabStream);

            try
            {
                this.Erf.Clear();
                NativeMethods.FDI.CABINFO fdici;
                bool isCabinet = 0 != NativeMethods.FDI.IsCabinet(this.fdiHandle, streamHandle, out fdici);

                if (this.Erf.Error)
                {
                    if (((NativeMethods.FDI.ERROR) this.Erf.Oper) == NativeMethods.FDI.ERROR.UNKNOWN_CABINET_VERSION)
                    {
                        isCabinet = false;
                    }
                    else
                    {
                        throw new CabException(
                                  this.Erf.Oper,
                                  this.Erf.Type,
                                  CabException.GetErrorMessage(this.Erf.Oper, this.Erf.Type, true));
                    }
                }

                id             = fdici.setID;
                cabFolderCount = (int)fdici.cFolders;
                fileCount      = (int)fdici.cFiles;
                return(isCabinet);
            }
            finally
            {
                this.StreamHandles.FreeHandle(streamHandle);
            }
        }
        private bool IsCabinet(Stream cabStream, out short id, out int cabFolderCount, out int fileCount)
        {
            int num = base.StreamHandles.AllocHandle(cabStream);

            try
            {
                base.Erf.Clear();
                NativeMethods.FDI.CABINFO pfdici;
                bool result = NativeMethods.FDI.IsCabinet(fdiHandle, num, out pfdici) != 0;
                if (base.Erf.Error)
                {
                    if (base.Erf.Oper != 3)
                    {
                        throw new CabException(base.Erf.Oper, base.Erf.Type, CabException.GetErrorMessage(base.Erf.Oper, base.Erf.Type, extracting: true));
                    }
                    result = false;
                }
                id             = pfdici.setID;
                cabFolderCount = pfdici.cFolders;
                fileCount      = pfdici.cFiles;
                return(result);
            }
            finally
            {
                base.StreamHandles.FreeHandle(num);
            }
        }
Пример #4
0
 protected void CheckError(bool extracting)
 {
     if (Erf.Error)
     {
         throw new CabException(Erf.Oper, Erf.Type, CabException.GetErrorMessage(Erf.Oper, Erf.Type, extracting));
     }
 }
 public CabUnpacker(CabEngine cabEngine)
     : base(cabEngine)
 {
     fdiAllocMemHandler    = base.CabAllocMem;
     fdiFreeMemHandler     = base.CabFreeMem;
     fdiOpenStreamHandler  = base.CabOpenStream;
     fdiReadStreamHandler  = base.CabReadStream;
     fdiWriteStreamHandler = base.CabWriteStream;
     fdiCloseStreamHandler = base.CabCloseStream;
     fdiSeekStreamHandler  = base.CabSeekStream;
     fdiHandle             = NativeMethods.FDI.Create(fdiAllocMemHandler, fdiFreeMemHandler, fdiOpenStreamHandler, fdiReadStreamHandler, fdiWriteStreamHandler, fdiCloseStreamHandler, fdiSeekStreamHandler, 1, base.ErfHandle.AddrOfPinnedObject());
     if (base.Erf.Error)
     {
         int oper = base.Erf.Oper;
         int type = base.Erf.Type;
         base.ErfHandle.Free();
         throw new CabException(oper, type, CabException.GetErrorMessage(oper, type, extracting: true));
     }
 }
Пример #6
0
        public void CabInfoSerialization()
        {
            CabInfo cabInfo = new CabInfo("testser.cab");
            int txtSize = 10240;
            CompressionTestUtil.GenerateRandomFile("testser00.txt", 0, txtSize);
            CompressionTestUtil.GenerateRandomFile("testser01.txt", 1, txtSize);
            cabInfo.PackFiles(null, new string[] { "testser00.txt", "testser01.txt" }, null);
            ArchiveFileInfo cfi = cabInfo.GetFiles()[1];

            MemoryStream memStream = new MemoryStream();

            BinaryFormatter formatter = new BinaryFormatter();

            memStream.Seek(0, SeekOrigin.Begin);
            formatter.Serialize(memStream, cabInfo);
            memStream.Seek(0, SeekOrigin.Begin);
            CabInfo cabInfo2 = (CabInfo) formatter.Deserialize(memStream);
            Assert.AreEqual<string>(cabInfo.FullName, cabInfo2.FullName);

            memStream.Seek(0, SeekOrigin.Begin);
            formatter.Serialize(memStream, cfi);
            memStream.Seek(0, SeekOrigin.Begin);
            CabFileInfo cfi2 = (CabFileInfo) formatter.Deserialize(memStream);
            Assert.AreEqual<string>(cfi.FullName, cfi2.FullName);
            Assert.AreEqual<long>(cfi.Length, cfi2.Length);

            CabException cabEx = new CabException();
            memStream.Seek(0, SeekOrigin.Begin);
            formatter.Serialize(memStream, cabEx);
            memStream.Seek(0, SeekOrigin.Begin);
            formatter.Deserialize(memStream);

            cabEx = new CabException("Test exception.", null);
            Assert.AreEqual<string>("Test exception.", cabEx.Message);
        }