【发布时间】:2011-05-03 04:54:13
【问题描述】:
基本上,我正在尝试将文件“sample.doc”压缩为 .gz 文件格式。发生这种情况时,系统会告诉它删除文件的扩展名,而不是显示为 “sample.doc.gz”显示为“sample.gz”。但是,当文件被提取时,它也丢失了它的“.doc”文件扩展名。例如。文件名只是“样本”。有什么想法吗?
使用系统; 使用 System.IO; 使用 System.IO.Compression; 使用 System.Text;
命名空间 gzip 示例 {
class Program
{
public static void Main()
{
CompressFile(@"C:\sample.doc");
}
//Compresses the file into a .gz file
public static void CompressFile(string path)
{
string compressedPath = path;
Console.WriteLine("Compressing: " + path);
int extIndex = compressedPath.LastIndexOf(".");
FileStream sourceFile = File.OpenRead(path);
FileStream destinationFile = File.Create(compressedPath.Replace(compressedPath.Substring(extIndex), "") + ".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();
}
}
}
【问题讨论】:
标签: c# gzip filenames compression