摘:C#压缩文件
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了摘:C#压缩文件,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3356字,纯文字阅读大概需要5分钟。
内容图文
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
|
【【【【C#压缩文件】】】】
方法1:
//【filepath想要压缩文件的地址】
//【zippath输出压缩文件的地址】
private
void
GetFileToZip(
string
filepath,
string
zippath)
{
FileStream fs = File.OpenRead(filepath);
byte
[] buffer =
new
byte
[fs.Length];
fs.Read(buffer, 0, buffer.Length);
fs.Close();
FileStream ZipFile = File.Create(zippath);
ZipOutputStream ZipStream =
new
ZipOutputStream(ZipFile);
ZipEntry ZipEntry =
new
ZipEntry(输出的文件名称);
ZipStream.PutNextEntry(ZipEntry);
ZipStream.SetLevel(6);
ZipStream.Write(buffer, 0, buffer.Length);
ZipStream.Finish();
ZipStream.Close();
}
方法2:
private
void
FileToZip(
string
path,
string
address)
{
string
name = openFileDialog1.FileName.Substring(openFileDialog1.FileName.LastIndexOf(
"\\"
)+1);
FileStream StreamToZip =
new
FileStream(path, FileMode.Open, FileAccess.Read);
FileStream ZipFile = File.Create(address);
ZipOutputStream ZipStream =
new
ZipOutputStream(ZipFile);
//压缩文件
ZipEntry ZipEntry =
new
ZipEntry(name);
ZipStream.PutNextEntry(ZipEntry);
ZipStream.SetLevel(6);
byte
[] buffer =
new
byte
[StreamToZip.Length];
StreamToZip.Read(buffer, 0, Convert.ToInt32(StreamToZip.Length));
ZipStream.Write(buffer, 0, Convert.ToInt32(StreamToZip.Length));
ZipStream.Finish();
ZipStream.Close();
StreamToZip.Close();
}
【【【【【【C#解压文件】】】】】】
private
void
ZipToFile(
string
path,
string
addres)
{
ZipInputStream s =
new
ZipInputStream(File.OpenRead(path));
ZipEntry fileEntry;
while
((fileEntry = s.GetNextEntry()) !=
null
)
{
string
filename = Path.GetFileName(fileEntry.Name);
if
(filename !=
""
)
{
filename = addres +
"\\"
+ filename;
FileStream streamWriter = File.Create(filename);
int
size = 2048;
byte
[] buffer =
new
byte
[s.length];
size = s.Read(buffer, 0, size);
streamWriter.Write(buffer, 0, size);
streamWriter.Close();
}
}
s.Close();
}
【【【【【【C#压缩目录】】】】】】
方法1:
//【arg[0]要压缩的目录】
//【arg[1]输出的压缩文件】
private
void
DirectoryToZip(
string
path,
string
address)
{
//获取当前文件夹中所有的文件
string
[] filenames = Directory.GetFiles(path);
Crc32 crc =
new
Crc32();
//创建输出文件(ZIP格式的文件)
ZipOutputStream zos =
new
ZipOutputStream(File.Create(address));
zos.SetLevel(6);
//遍历所有的文件
foreach
(
string
name
in
filenames)
{
FileStream fs = File.OpenRead(name);
byte
[] buffer =
new
byte
[fs.Length];
//读取文件
fs.Read(buffer, 0, Convert.ToInt32(fs.Length));
//获取文件的文件名称和后缀名
string
file = Path.GetFileName(name);
//输出文件的名称
ZipEntry entry =
new
ZipEntry(file);
crc.Reset();
crc.Update(buffer);
entry.Crc = crc.Value;
zos.PutNextEntry(entry);
zos.Write(buffer, 0, Convert.ToInt32(fs.Length));
fs.Close();
}
zos.Finish();
zos.Close();
}
【【【【【【【【C#读取压缩文件(将压缩文件转换为二进制)】】】】】】】】
private
void
GetZipToByte(){
string
path =
@"C:\Documents and Settings\Administrator\桌面\文件.rar"
;
FileStream fs =
new
FileStream(path, FileMode.Open);
bytes =
new
byte
[fs.Length];
int
count = Convert.ToInt32(fs.Length);
fs.Read(bytes, 0, count);
}
【【【【【【【【C#将二进制转换为压缩文件】】】】】】】】
private
void
GetByteToZip()
{
string
path =
@"F:\dom.rar"
;
//压缩文件的地址
File.WriteAllBytes(path, bytes);
}
|
原文:http://www.cnblogs.com/shenchao/p/3725634.html
内容总结
以上是互联网集市为您收集整理的摘:C#压缩文件全部内容,希望文章能够帮你解决摘:C#压缩文件所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。