C # Implementation of rar compression and decompression of Files

Source: Internet
Author: User
Tags time in milliseconds

C # Implementation of rar compression and decompression of Files

This document describes how to compress and decompress files in C. Share it with you for your reference. The specific analysis is as follows:

This program uses the WinRAR program to compress the file. For command line syntax, see WinRAR help.

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

/// Use WinRAR for compression

/// </Summary>

/// <Param name = "path"> folder to be compressed (absolute path) </param>

/// <Param name = "rarPath"> compressed. rar directory (absolute path) </param>

/// <Param name = "rarName"> name (including suffix) of the compressed file </param>

/// <Returns> true or false. True is returned if compression is successful. Otherwise, false is returned. </Returns>

Public bool RAR (string path, string rarPath, string rarName)

{

Bool flag = false;

String rarexe; // The complete path of WinRAR.exe

RegistryKey regkey; // registry key

Object regvalue; // key value

String cmd; // WinRAR Command Parameters

ProcessStartInfo startinfo;

Process process;

Try

{

Regkey = Registry. ClassesRoot. OpenSubKey (@ "Applications \ WinRAR.exe \ shell \ open \ command ");

Regvalue = regkey. GetValue (""); // the key value is "d: \ Program Files \ WinRAR \ WinRAR.exe" "% 1"

Rarexe = regvalue. ToString ();

Regkey. Close ();

Rarexe = rarexe. Substring (1, rarexe. Length-7); // d: \ Program Files \ WinRAR \ WinRAR.exe

Directory. CreateDirectory (path );

// The compression command, which is equivalent to right-clicking on the folder (path) to be compressed-> WinRAR-> adding to the compressed file-> entering the compressed file name (rarName)

Cmd = string. Format ("a {0} {1}-r", rarName, path );

Startinfo = new ProcessStartInfo ();

Startinfo. FileName = rarexe;

Startinfo. Arguments = cmd; // set Command Parameters

Startinfo. WindowStyle = ProcessWindowStyle. Hidden; // hide the WinRAR window

Startinfo. WorkingDirectory = rarPath;

Process = new Process ();

Process. StartInfo = startinfo;

Process. Start ();

Process. WaitForExit (); // wait for the winrar.exe process to exit indefinitely

If (process. HasExited)

{

Flag = true;

}

Process. Close ();

}

Catch (Exception e)

{

Throw e;

}

Return flag;

}

/// <Summary>

/// Decompress the package using WinRAR

/// </Summary>

/// <Param name = "path"> extract the file path (absolute) </param>

/// <Param name = "rarPath"> storage directory of the. rar file to be decompressed (absolute path) </param>

/// <Param name = "rarName"> name Of The. rar file to be decompressed (including the suffix) </param>

/// <Returns> true or false. True is returned for successful decompression. Otherwise, false is returned. </Returns>

Public bool UnRAR (string path, string rarPath, string rarName)

{

Bool flag = false;

String rarexe;

RegistryKey regkey;

Object regvalue;

String cmd;

ProcessStartInfo startinfo;

Process process;

Try

{

Regkey = Registry. ClassesRoot. OpenSubKey (@ "Applications \ WinRAR.exe \ shell \ open \ command ");

Regvalue = regkey. GetValue ("");

Rarexe = regvalue. ToString ();

Regkey. Close ();

Rarexe = rarexe. Substring (1, rarexe. Length-7 );

Directory. CreateDirectory (path );

// Decompress the command, which is equivalent to right-clicking on the file to be compressed (rarName)-> WinRAR-> decompressing to the current folder

Cmd = string. Format ("x {0} {1}-y", rarName, path );

Startinfo = new ProcessStartInfo ();

Startinfo. FileName = rarexe;

Startinfo. Arguments = cmd;

Startinfo. WindowStyle = ProcessWindowStyle. Hidden;

Startinfo. WorkingDirectory = rarPath;

Process = new Process ();

Process. StartInfo = startinfo;

Process. Start ();

Process. WaitForExit ();

If (process. HasExited)

{

Flag = true;

}

Process. Close ();

}

Catch (Exception e)

{

Throw e;

}

Return flag;

}

/// <Summary>

/// Use WinRAR for compression

/// </Summary>

/// <Param name = "path"> folder to be compressed (absolute path) </param>

/// <Param name = "rarPath"> compressed. rar directory (absolute path) </param>

/// <Param name = "rarName"> name (including suffix) of the compressed file </param>

/// <Returns> true or false. True is returned if compression is successful. Otherwise, false is returned. </Returns>

Public bool RAR (string path, string rarPath, string rarName)

{

Bool flag = false;

String rarexe; // The complete path of WinRAR.exe

RegistryKey regkey; // registry key

Object regvalue; // key value

String cmd; // WinRAR Command Parameters

ProcessStartInfo startinfo;

Process process;

Try

{

Regkey = Registry. ClassesRoot. OpenSubKey (@ "Applications \ WinRAR.exe \ shell \ open \ command ");

Regvalue = regkey. GetValue (""); // the key value is "d: \ Program Files \ WinRAR \ WinRAR.exe" "% 1"

Rarexe = regvalue. ToString ();

Regkey. Close ();

Rarexe = rarexe. Substring (1, rarexe. Length-7); // d: \ Program Files \ WinRAR \ WinRAR.exe

Directory. CreateDirectory (path );

// The compression command, which is equivalent to right-clicking on the folder (path) to be compressed-> WinRAR-> adding to the compressed file-> entering the compressed file name (rarName)

Cmd = string. Format ("a {0} {1}-r", rarName, path );

Startinfo = new ProcessStartInfo ();

Startinfo. FileName = rarexe;

Startinfo. Arguments = cmd; // set Command Parameters

Startinfo. WindowStyle = ProcessWindowStyle. Hidden; // hide the WinRAR window

Startinfo. WorkingDirectory = rarPath;

Process = new Process ();

Process. StartInfo = startinfo;

Process. Start ();

Process. WaitForExit (); // wait for the winrar.exe process to exit indefinitely

If (process. HasExited)

{

Flag = true;

}

Process. Close ();

}

Catch (Exception e)

{

Throw e;

}

Return flag;

}

/// <Summary>

/// Decompress the package using WinRAR

/// </Summary>

/// <Param name = "path"> extract the file path (absolute) </param>

/// <Param name = "rarPath"> storage directory of the. rar file to be decompressed (absolute path) </param>

/// <Param name = "rarName"> name Of The. rar file to be decompressed (including the suffix) </param>

/// <Returns> true or false. True is returned for successful decompression. Otherwise, false is returned. </Returns>

Public bool UnRAR (string path, string rarPath, string rarName)

{

Bool flag = false;

String rarexe;

RegistryKey regkey;

Object regvalue;

String cmd;

ProcessStartInfo startinfo;

Process process;

Try

{

Regkey = Registry. ClassesRoot. OpenSubKey (@ "Applications \ WinRAR.exe \ shell \ open \ command ");

Regvalue = regkey. GetValue ("");

Rarexe = regvalue. ToString ();

Regkey. Close ();

Rarexe = rarexe. Substring (1, rarexe. Length-7 );

Directory. CreateDirectory (path );

// Decompress the command, which is equivalent to right-clicking on the file to be compressed (rarName)-> WinRAR-> decompressing to the current folder

Cmd = string. Format ("x {0} {1}-y", rarName, path );

Startinfo = new ProcessStartInfo ();

Startinfo. FileName = rarexe;

Startinfo. Arguments = cmd;

Startinfo. WindowStyle = ProcessWindowStyle. Hidden;

Startinfo. WorkingDirectory = rarPath;

Process = new Process ();

Process. StartInfo = startinfo;

Process. Start ();

Process. WaitForExit ();

If (process. HasExited)

{

Flag = true;

}

Process. Close ();

}

Catch (Exception e)

{

Throw e;

}

Return flag;

}

Run the cd command in dos or cmd to enter the winrar installation directory. Enter unrar and the following prompt is displayed:

Usage: unrar <command>-<switch 1>-<switch N> <compressed file> <file...>

<@ List file...> <decompress path \>

<Command>

E. decompress the compressed file to the current directory.

L [t, B] list compressed files [technical information, concise]

P print the file to the standard output device

T test compressed files

V [t, B] detailed list of compressed files [technical information, concise]

X decompress the file using the absolute path

<Switch>

-Stop Scanning

After the ac is compressed or unzipped, the archive attributes are cleared.

Ad: Add compressed file names to the target path

Ap <format> Add path to compressed file

Av-Disable User identity verification

C-Disable annotation display

Cfg-Disable read Configuration

Cl name to lowercase

Convert cu name to uppercase

Dh open shared files

Ep exclusion path from name

The ep3 extension path is the full path containing the drive letter.

F. Refresh the file

Id [c, d, p, q] disable messages

Ierr sends all messages to standard error Devices

Inul disables all messages

After ioff completes an operation, turn off the PC power supply.

Kb retains damaged decompressed files

N <File> only contains the specified file

N @ read the file name from the standard input device to include

N @ <list> include files in the specified file list

O + overwrite existing files

O-Do Not Overwrite existing files

Set NTFS compression attribute for oc

Or automatically rename the file

Ow save or restore the file owner and group

P [Password] Set Password

P-No Password inquiry

R recursive subdirectory

Ri

[:] Set the priority (0-default, 1-minimum... 15-maximum) and sleep time in milliseconds

Sl <size> process files smaller than the specified size

Sm <size> processes files larger than the specified size

File Modified after ta <date> Add date <date>, in the format of YYYYMMDDHHMMSS

File Modified before tb <date> Add date <date>, in the format of YYYYMMDDHHMMSS

Files after tn <time> Add <time>

To <time> add files earlier than <time>

Ts [N] file retention or recovery time (modify, create, access)

U Update file

V. list all volumes

Ver [n] file version control

Vp is paused before each volume

X <File> exclude specified files

X @ read the file name to be excluded from the standard input device

X @ <list> exclude objects in a specified list file

Y. If yes

Example of how to use WinRAR to decompress a file in DOS (cmd:

Suppose there is a winrar.rar in drive d. to decompress it to the hello folder in drive F, enter the following command under dos:

Unrar x d: \ winrar.rar F: \ hello \

I hope this article will help you with C # programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.