Exemplo n.º 1
0
 public void Dispose()
 {
     if (this.archiveHandle != IntPtr.Zero)
     {
         Rar.RARCloseArchive(this.archiveHandle);
         this.archiveHandle = IntPtr.Zero;
     }
 }
Exemplo n.º 2
0
        private void Extract(string destinationPath, string destinationName)
        {
            int result = Rar.RARProcessFile(this.archiveHandle, (int)Operation.Extract, destinationPath, destinationName);

            // Check result
            if (result != 0)
            {
                ProcessFileError(result);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Tests the ability to extract the current file without saving extracted data to disk
        /// </summary>
        /// <returns></returns>
        public void Test()
        {
            int result = Rar.RARProcessFile(this.archiveHandle, (int)Operation.Test, string.Empty, string.Empty);

            // Check result
            if (result != 0)
            {
                ProcessFileError(result);
            }
        }
Exemplo n.º 4
0
        internal static string UnCompressFiles(string filePath, string unCompressDirName, bool isCoverOrNew)
        {
            filePath = filePath.Replace('/', '\\');
            string dirPath  = filePath.Substring(0, filePath.LastIndexOf('\\') + 1).Trim('\\'); //文件夹
            string fileName = filePath.Substring(filePath.LastIndexOf('\\')).Trim('\\');        //文件名

            if (!isCoverOrNew)
            {
                if (string.IsNullOrEmpty(unCompressDirName))
                {
                    unCompressDirName = fileName.Substring(0, fileName.LastIndexOf('.'));
                }
                dirPath = ALFile.GetNewDirPath(dirPath, unCompressDirName, isCoverOrNew);
            }
            bool isNewDir = false;

            if (!Directory.Exists(dirPath))
            {
                Directory.CreateDirectory(dirPath);
                isNewDir = true;
            }
            Rar rar = new Rar();

            try
            {
                rar.PasswordRequired += (sender, eventArgs) => { throw new Exception("不能解压设置密码的文件!"); };
                rar.MissingVolume    += (sender, eventArgs) => { throw new Exception("压缩文件已损坏!"); };
                rar.DestinationPath   = dirPath;
                rar.Open(filePath, OpenMode.Extract);
                while (rar.ReadHeader())
                {
                    rar.Extract();
                }
            }
            catch (Exception ex)
            {
                if (isNewDir)
                {
                    Directory.Delete(dirPath, true);
                }
                throw ex;
            }
            finally
            {
                if (rar != null)
                {
                    rar.Close();
                }
            }
            return(dirPath);
        }
Exemplo n.º 5
0
        /// <summary>
        /// 解压文件主方法 支持所有格式的解压
        /// </summary>
        /// <param name="filePath">压缩文件路径</param>
        /// <param name="unCompressDirName">解压目录名称</param>
        /// <param name="isCoverOrNew">true为覆盖,false为新建</param>
        /// <returns></returns>
        public static string UnCompressFiles(string filePath, string unCompressDirName, bool isCoverOrNew)
        {
            if (string.IsNullOrEmpty(filePath))
            {
                throw new Exception("请传入需解压的文件绝对路径(filePath)!");
            }
            string suffixName = filePath.Substring(filePath.LastIndexOf('.') + 1).ToUpper();

            if (suffixName == "ZIP" || suffixName == "GZIP" || suffixName == "TAR" || suffixName == "BZIP2")
            {
                return(Zip.UnCompressFiles(filePath, unCompressDirName, isCoverOrNew));
            }
            else
            {
                return(Rar.UnCompressFiles(filePath, unCompressDirName, isCoverOrNew));
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// Close the currently open archive
        /// </summary>
        /// <returns></returns>
        public void Close()
        {
            // Exit without exception if no archive is open
            if (this.archiveHandle == IntPtr.Zero)
            {
                return;
            }

            // Close archive
            int result = Rar.RARCloseArchive(this.archiveHandle);

            // Check result
            if (result != 0)
            {
                ProcessFileError(result);
            }
            else
            {
                this.archiveHandle = IntPtr.Zero;
            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// Reads the next archive header and populates CurrentFile property data
        /// </summary>
        /// <returns></returns>
        public bool ReadHeader()
        {
            // Throw exception if archive not open
            if (this.archiveHandle == IntPtr.Zero)
            {
                throw new IOException("Archive is not open.");
            }

            // Initialize header struct
            this.header = new RARHeaderDataEx();
            header.Initialize();

            // Read next entry
            currentFile = null;
            int result = Rar.RARReadHeaderEx(this.archiveHandle, ref this.header);

            // Check for error or end of archive
            if ((RarError)result == RarError.EndOfArchive)
            {
                return(false);
            }
            else if ((RarError)result == RarError.BadData)
            {
                throw new IOException("Archive data is corrupt.");
            }

            // Determine if new file
            if (((header.Flags & 0x01) != 0) && currentFile != null)
            {
                currentFile.ContinuedFromPrevious = true;
            }
            else
            {
                // New file, prepare header
                currentFile          = new RARFileInfo();
                currentFile.FileName = header.FileNameW.ToString();
                if ((header.Flags & 0x02) != 0)
                {
                    currentFile.ContinuedOnNext = true;
                }
                if (header.PackSizeHigh != 0)
                {
                    currentFile.PackedSize = (header.PackSizeHigh * 0x100000000) + header.PackSize;
                }
                else
                {
                    currentFile.PackedSize = header.PackSize;
                }
                if (header.UnpSizeHigh != 0)
                {
                    currentFile.UnpackedSize = (header.UnpSizeHigh * 0x100000000) + header.UnpSize;
                }
                else
                {
                    currentFile.UnpackedSize = header.UnpSize;
                }
                currentFile.HostOS          = (int)header.HostOS;
                currentFile.FileCRC         = header.FileCRC;
                currentFile.FileTime        = FromMSDOSTime(header.FileTime);
                currentFile.VersionToUnpack = (int)header.UnpVer;
                currentFile.Method          = (int)header.Method;
                currentFile.FileAttributes  = (int)header.FileAttr;
                currentFile.BytesExtracted  = 0;
                if ((header.Flags & 0xE0) == 0xE0)
                {
                    currentFile.IsDirectory = true;
                }
                this.OnNewFile();
            }

            // Return success
            return(true);
        }
Exemplo n.º 8
0
        /// <summary>
        /// Opens specified archive using the specified mode.
        /// </summary>
        /// <param name="archivePathName">Path of archive to open</param>
        /// <param name="openMode">Mode in which to open archive</param>
        public void Open(string archivePathName, OpenMode openMode)
        {
            IntPtr handle = IntPtr.Zero;

            // Close any previously open archives
            if (this.archiveHandle != IntPtr.Zero)
            {
                this.Close();
            }

            // Prepare extended open archive struct
            this.ArchivePathName = archivePathName;
            RAROpenArchiveDataEx openStruct = new RAROpenArchiveDataEx();

            openStruct.Initialize();
            openStruct.ArcName  = this.archivePathName + "\0";
            openStruct.ArcNameW = this.archivePathName + "\0";
            openStruct.OpenMode = (uint)openMode;
            if (this.retrieveComment)
            {
                openStruct.CmtBuf     = new string((char)0, 65536);
                openStruct.CmtBufSize = 65536;
            }
            else
            {
                openStruct.CmtBuf     = null;
                openStruct.CmtBufSize = 0;
            }

            // Open archive
            handle = Rar.RAROpenArchiveEx(ref openStruct);

            // Check for success
            if (openStruct.OpenResult != 0)
            {
                switch ((RarError)openStruct.OpenResult)
                {
                case RarError.InsufficientMemory:
                    throw new OutOfMemoryException("Insufficient memory to perform operation.");

                case RarError.BadData:
                    throw new IOException("Archive header broken");

                case RarError.BadArchive:
                    throw new IOException("File is not a valid archive.");

                case RarError.OpenError:
                    throw new IOException("File could not be opened.");
                }
            }

            // Save handle and flags
            this.archiveHandle = handle;
            this.archiveFlags  = (ArchiveFlags)openStruct.Flags;

            // Set callback
            Rar.RARSetCallback(this.archiveHandle, this.callback, this.GetHashCode());

            // If comment retrieved, save it
            if (openStruct.CmtState == 1)
            {
                this.comment = openStruct.CmtBuf.ToString();
            }

            // If password supplied, set it
            if (this.password.Length != 0)
            {
                Rar.RARSetPassword(this.archiveHandle, this.password);
            }

            // Fire NewVolume event for first volume
            this.OnNewVolume(this.archivePathName);
        }