ホームページ > バックエンド開発 > C#.Net チュートリアル > C# ファイルストリームの圧縮と解凍

C# ファイルストリームの圧縮と解凍

黄舟
リリース: 2017-02-13 11:52:28
オリジナル
2743 人が閲覧しました

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

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

/// <summary>

   /// 文件流压缩解压

   /// </summary>

   public class ZipHelper

   {

       public static int BEST_COMPRESSION = 9;

       public static int BEST_SPEED = 1;

       public static int DEFAULT_COMPRESSION = -1;

       public static int NO_COMPRESSION = 0;

 

       #region  Deflate压缩

 

       #region Deflate压缩

       /// <summary>

       /// Deflate方式压缩(默认压缩级别最高)

       /// </summary>

       /// <param name="stream"></param>

       /// <returns></returns>

       public static Stream Deflate(Stream stream)

       {

           return ZipHelper.Deflate(stream, ZipHelper.DEFAULT_COMPRESSION);

       }

       /// <summary>

       ///  Deflate方式压缩

       /// </summary>

       /// <param name="stream"></param>

       /// <param name="level">压缩品质级别(0~9)</param>

       /// <returns></returns>

       public static Stream Deflate(Stream stream, int level)

       {

           byte[] array = ZipHelper.StreamToBytes(stream);

           byte[] array2 = new byte[array.Length];

           Deflater deflater = new Deflater();

           deflater.SetLevel(level);

           deflater.SetStrategy(DeflateStrategy.Default);

           deflater.SetInput(array);

           deflater.Finish();

           int num = deflater.Deflate(array2);

           byte[] array3 = new byte[num];

           Array.Copy(array2, array3, num);

           return ZipHelper.BytesToStream(array3);

       }

 

       /// <summary>

       /// Deflate方式压缩

       /// </summary>

       /// <param name="input"></param>

       /// <param name="level">压缩品质级别(0~9)</param>

       /// <returns></returns>

       public static byte[] Deflate(byte[] input, int level)

       {

           byte[] result;

           try

           {

               if (input == null && input.Length == 0)

               {

                   result = new byte[0];

               }

               else

               {

                   byte[] array = new byte[input.Length];

                   Deflater deflater = new Deflater();

                   deflater.SetLevel(level);

                   deflater.SetStrategy(DeflateStrategy.Default);

                   deflater.SetInput(input);

                   deflater.Finish();

                   int num = deflater.Deflate(array);

                   byte[] array2 = new byte[num];

                   Array.Copy(array, array2, num);

                   result = array2;

               }

           }

           catch (Exception innerException)

           {

               throw new Exception("压缩程序出错!", innerException);

           }

           return result;

       }

       #endregion

 

       #region Inflate解压

       /// <summary>

       /// Inflate解压

       /// </summary>

       /// <param name="input"></param>

       /// <returns></returns>

       public static byte[] Inflate(byte[] input)

       {

           byte[] result;

           try

           {

               if (input == null && input.Length == 0)

               {

                   result = new byte[0];

               }

               else

               {

                   Inflater inflater = new Inflater();

                   inflater.SetInput(input);

                   byte[] array = new byte[1024];

                   using (MemoryStream memoryStream = new MemoryStream())

                   {

                       for (int i = inflater.Inflate(array, 0, array.Length); i > 0; i = inflater.Inflate(array, 0, array.Length))

                       {

                           memoryStream.Write(array, 0, i);

                       }

                       byte[] buffer = memoryStream.GetBuffer();

                       memoryStream.Close();

                       result = buffer;

                   }

               }

           }

           catch (Exception innerException)

           {

               throw new Exception("解压缩程序出错!", innerException);

           }

           return result;

       }

       /// <summary>

       /// Inflate解压

       /// </summary>

       /// <param name="zipStream"></param>

       /// <returns></returns>

       public static Stream Inflate(Stream zipStream)

       {

           byte[] input = ZipHelper.StreamToBytes(zipStream);

           byte[] bytes = ZipHelper.Inflate(input);

           return ZipHelper.BytesToStream(bytes);

       }

       #endregion

 

       #endregion

 

       #region GZip压缩

       /// <summary>

       /// GZip压缩

       /// </summary>

       /// <param name="srcStream"></param>

       /// <param name="output"></param>

       public static void GZipCompress(Stream srcStream, Stream output)

       {

           ZipHelper.GZipCompress(srcStream, 6, output);

       }

       /// <summary>

       ///  GZip压缩

       /// </summary>

       /// <param name="srcStream"></param>

       /// <param name="compressLevel">压缩品质级别(0~9)</param>

       /// <param name="output"></param>

       public static void GZipCompress(Stream srcStream, int compressLevel, Stream output)

       {

           if (compressLevel < 1 || compressLevel > 9)

           {

               throw new Exception(string.Format("您指定的压缩级别 {0} 不在有效的范围(1-9)内", compressLevel));

           }

           srcStream.Position = 0L;

           GZipOutputStream gZipOutputStream = new GZipOutputStream(output);

           gZipOutputStream.SetLevel(compressLevel);

           try

           {

               int i = 4096;

               byte[] buffer = new byte[i];

               while (i > 0)

               {

                   i = srcStream.Read(buffer, 0, i);

                   gZipOutputStream.Write(buffer, 0, i);

               }

           }

           catch (Exception ex)

           {

               throw new Exception("GZip压缩出错:" + ex.Message);

           }

           srcStream.Close();

           gZipOutputStream.Finish();

       }

       /// <summary>

       ///  GZip解压

       /// </summary>

       /// <param name="zipStream"></param>

       /// <param name="outputStream"></param>

       public static void GZipDeCompress(Stream zipStream, Stream outputStream)

       {

           GZipInputStream gZipInputStream = new GZipInputStream(zipStream);

           try

           {

               int i = 4096;

               byte[] buffer = new byte[i];

               while (i > 0)

               {

                   i = gZipInputStream.Read(buffer, 0, i);

                   outputStream.Write(buffer, 0, i);

               }

           }

           catch (Exception ex)

           {

               throw new Exception("GZip解压缩出错:" + ex.Message);

           }

           zipStream.Close();

           gZipInputStream.Close();

       }

       #endregion

 

       #region  BZip2压缩

       /// <summary>

       /// BZip2压缩

       /// </summary>

       /// <param name="inStream"></param>

       /// <param name="outStream"></param>

       /// <param name="blockSize"></param>

       public static void BZip2Compress(Stream inStream, Stream outStream, int blockSize)

       {

           BZip2.Compress(inStream, outStream, blockSize);

       }

       /// <summary>

       /// BZip2解压

       /// </summary>

       /// <param name="inStream"></param>

       /// <param name="outStream"></param>

       public static void BZip2Decompress(Stream inStream, Stream outStream)

       {

           BZip2.Decompress(inStream, outStream);

       }

       #endregion

 

 

       private static byte[] StreamToBytes(Stream stream)

       {

           byte[] array = new byte[stream.Length];

           stream.Seek(0L, SeekOrigin.Begin);

           stream.Read(array, 0, array.Length);

           stream.Close();

           return array;

       }

       private static Stream BytesToStream(byte[] bytes)

       {

           return new MemoryStream(bytes);

       }

       private static void StreamToFile(Stream stream, string fileName)

       {

           byte[] array = new byte[stream.Length];

           stream.Read(array, 0, array.Length);

           stream.Seek(0L, SeekOrigin.Begin);

           FileStream fileStream = new FileStream(fileName, FileMode.Create);

           BinaryWriter binaryWriter = new BinaryWriter(fileStream);

           binaryWriter.Write(array);

           binaryWriter.Close();

           fileStream.Close();

       }

       private static Stream FileToStream(string fileName)

       {

           FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);

           byte[] array = new byte[fileStream.Length];

           fileStream.Read(array, 0, array.Length);

           fileStream.Close();

           return new MemoryStream(array);

       }

   }

ログイン後にコピー

上記は C# ファイル ストリームの圧縮と解凍の内容です。さらに関連する内容については、PHP 中国語 Web サイト (www.php.cn) に注目してください。

関連ラベル:
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート