基本文件的I/O --压缩文件
2015-08-28 16:56
316 查看
使用 GZipStream 类压缩和解压缩数据。下面的代码示例在当前目录中创建一个文件 (test.txt),在其中添加文本并将文件的内容显示到控制台。然后代码使用 GZipStream 类创建该文件的压缩版本
(test.txt.gz) 并比较两个文件的大小。最后,代码读取压缩的文件,将其解压并向当前目录写出一个新文件 (test.txt.gz.txt)。该代码然后显示解压缩文件的内容。也可以使用 DeflateStream 类压缩和解压缩数据。
public static void Main(string[] args)
{
string path = "test.txt";
// Create the text file if it doesn't already exist.
if (!File.Exists(path))
{
Console.WriteLine("Creating a new test.txt file");
string[] text = new string[] {"This is a test text file.",
"This file will be compressed and written to the disk.",
"Once the file is written, it can be decompressed",
"using various compression tools.",
"The GZipStream and DeflateStream class use the same",
"compression algorithms, the primary difference is that",
"the GZipStream class includes a cyclic redundancy check",
"that can be useful for detecting data corruption.",
"One other side note: both the GZipStream and DeflateStream",
"classes operate on streams as opposed to file-based",
"compression; data is read on a byte-by-byte basis, so it",
"is not possible to perform multiple passes to determine the",
"best compression method. Already compressed data can actually",
"increase in size if compressed with these classes."};
File.WriteAllLines(path, text);
//Console.ReadKey();
}
Console.WriteLine("Contents of {0}", path);
Console.WriteLine(File.ReadAllText(path));
CompressFile(path);//压缩
Console.WriteLine();
UncompressFile(path + ".gz");//解压
Console.WriteLine();
Console.WriteLine("Contents of {0}", path + ".gz.txt");
Console.WriteLine(File.ReadAllText(path + ".gz.txt"));
Console.ReadKey();
}
public static void CompressFile(string path)
{
FileStream sourceFile = File.OpenRead(path);
FileStream destinationFile = File.Create(path + ".gz");
byte[] buffer = new byte[sourceFile.Length];
sourceFile.Read(buffer, 0, buffer.Length);
using (GZipStream output = new GZipStream(destinationFile,
CompressionMode.Compress))
{
Console.WriteLine("Compressing {0} to {1}.", sourceFile.Name,
destinationFile.Name, false);
output.Write(buffer, 0, buffer.Length);
}
// Close the files.
sourceFile.Close();
destinationFile.Close();
}
public static void UncompressFile(string path)
{
FileStream sourceFile = File.OpenRead(path);
FileStream destinationFile = File.Create(path + ".txt");
// Because the uncompressed size of the file is unknown,
// we are using an arbitrary buffer size.
byte[] buffer = new byte[4096];
int n;
using (GZipStream input = new GZipStream(sourceFile,
CompressionMode.Decompress, false))
{
Console.WriteLine("Decompressing {0} to {1}.", sourceFile.Name,
destinationFile.Name);
n = input.Read(buffer, 0, buffer.Length);
destinationFile.Write(buffer, 0, n);
}
// Close the files.
sourceFile.Close();
destinationFile.Close();
}
(test.txt.gz) 并比较两个文件的大小。最后,代码读取压缩的文件,将其解压并向当前目录写出一个新文件 (test.txt.gz.txt)。该代码然后显示解压缩文件的内容。也可以使用 DeflateStream 类压缩和解压缩数据。
public static void Main(string[] args)
{
string path = "test.txt";
// Create the text file if it doesn't already exist.
if (!File.Exists(path))
{
Console.WriteLine("Creating a new test.txt file");
string[] text = new string[] {"This is a test text file.",
"This file will be compressed and written to the disk.",
"Once the file is written, it can be decompressed",
"using various compression tools.",
"The GZipStream and DeflateStream class use the same",
"compression algorithms, the primary difference is that",
"the GZipStream class includes a cyclic redundancy check",
"that can be useful for detecting data corruption.",
"One other side note: both the GZipStream and DeflateStream",
"classes operate on streams as opposed to file-based",
"compression; data is read on a byte-by-byte basis, so it",
"is not possible to perform multiple passes to determine the",
"best compression method. Already compressed data can actually",
"increase in size if compressed with these classes."};
File.WriteAllLines(path, text);
//Console.ReadKey();
}
Console.WriteLine("Contents of {0}", path);
Console.WriteLine(File.ReadAllText(path));
CompressFile(path);//压缩
Console.WriteLine();
UncompressFile(path + ".gz");//解压
Console.WriteLine();
Console.WriteLine("Contents of {0}", path + ".gz.txt");
Console.WriteLine(File.ReadAllText(path + ".gz.txt"));
Console.ReadKey();
}
public static void CompressFile(string path)
{
FileStream sourceFile = File.OpenRead(path);
FileStream destinationFile = File.Create(path + ".gz");
byte[] buffer = new byte[sourceFile.Length];
sourceFile.Read(buffer, 0, buffer.Length);
using (GZipStream output = new GZipStream(destinationFile,
CompressionMode.Compress))
{
Console.WriteLine("Compressing {0} to {1}.", sourceFile.Name,
destinationFile.Name, false);
output.Write(buffer, 0, buffer.Length);
}
// Close the files.
sourceFile.Close();
destinationFile.Close();
}
public static void UncompressFile(string path)
{
FileStream sourceFile = File.OpenRead(path);
FileStream destinationFile = File.Create(path + ".txt");
// Because the uncompressed size of the file is unknown,
// we are using an arbitrary buffer size.
byte[] buffer = new byte[4096];
int n;
using (GZipStream input = new GZipStream(sourceFile,
CompressionMode.Decompress, false))
{
Console.WriteLine("Decompressing {0} to {1}.", sourceFile.Name,
destinationFile.Name);
n = input.Read(buffer, 0, buffer.Length);
destinationFile.Write(buffer, 0, n);
}
// Close the files.
sourceFile.Close();
destinationFile.Close();
}
相关文章推荐
- hdu 3400 Line belt(多重三分)
- 解决svn在win7下安装后右键无菜单项的问题
- 深入JVM系列(二)之GC机制、收集器与GC调优
- tomcat7配置环境变量
- java 死锁问题 代码实现
- 关于unity中的update、Lateupdate和FixedUpdate。
- android 粗暴简单仿美团/大众点评搜索方式,详细标注
- WIN7局域网文件共享设置方法
- C# vs C++ Performance
- iptebles(二)
- Eclipse 注释模板 函数注释 类注释 文件注释 Code Template - 2
- Linux中sort命令
- Deleting JPA Entity Objects
- Return from initializer without initializing all stored properties 错误
- 黑马程序员——Java之常用API
- Python daemonize
- linux下文件同步利器rsync
- Intellij Idea 创建JavaWeb项目
- hadoop2.7【单节点】单机、伪分布、分布式安装指导
- 002-可变字符串(NSMutableString)