C# 如何将zip文件解压缩为文本扩展名
可能的重复项:C# 如何将zip文件解压缩为文本扩展名,c#,C#,可能的重复项: 嗨 我在将zip文件解压缩为文本文件时遇到问题 当我将文本文件压缩为zip文件(Ex-20110530.txt到20110530.zip)时,我的代码正在压缩用于将文本文件压缩为zip文件的代码是 string path = (string)(Application.StartupPath + "\\TEMP\\TEMP_BACKFILL_atoz" + "\\" + name_atoz); StreamWriter Strwriter = new
嗨 我在将zip文件解压缩为文本文件时遇到问题 当我将文本文件压缩为zip文件(Ex-20110530.txt到20110530.zip)时,我的代码正在压缩用于将文本文件压缩为zip文件的代码是
string path = (string)(Application.StartupPath + "\\TEMP\\TEMP_BACKFILL_atoz" + "\\" + name_atoz);
StreamWriter Strwriter = new StreamWriter(path);
DirectoryInfo di = new DirectoryInfo(path);
FileInfo fi = new FileInfo(path);
Compress(fi);
public static void Compress(FileInfo fi)
{
// Get the stream of the source file.
using (FileStream inFile = fi.OpenRead())
{
// Prevent compressing hidden and already compressed files.
if ((File.GetAttributes(fi.FullName) & FileAttributes.Hidden)
!= FileAttributes.Hidden & fi.Name != ".zip")
{
// Create the compressed file.
using (FileStream outFile = File.Create(System.Text.RegularExpressions.Regex.Replace(fi.FullName, ".txt$", "") + ".zip"))
{
using (GZipStream Compress = new GZipStream(outFile,
CompressionMode.Compress))
{
// Copy the source file into the compression stream.
byte[] buffer = new byte[4096];
int numRead;
while ((numRead = inFile.Read(buffer, 0, buffer.Length)) != 0)
{
Compress.Write(buffer, 0, numRead);
}
Console.WriteLine("Compressed {0} from {1} to {2} bytes.",
fi.Name, fi.Length.ToString(), outFile.Length.ToString());
}
}
}
}
}
这段代码在解压文件时创建了一个zip文件,它创建了一个名为20110530的文件,而不是20110530.txt
我希望该文件以20110530.txt格式解压缩
我用来解压缩文件的代码是
public static void Decompress(FileInfo fi)
{
// Get the stream of the source file.
using (FileStream inFile = fi.OpenRead())
{
// Get original file extension, for example "doc" from report.doc.gz.
string curFile = fi.FullName;
string origName = curFile.Remove(curFile.Length - fi.Extension.Length);
//Create the decompressed file.
using (FileStream outFile = File.Create(origName))
{
using (GZipStream Decompress = new GZipStream(inFile,
CompressionMode.Decompress))
{
//Copy the decompression stream into the output file.
byte[] buffer = new byte[4096];
int numRead;
while ((numRead = Decompress.Read(buffer, 0, buffer.Length)) != 0)
{
outFile.Write(buffer, 0, numRead);
}
Console.WriteLine("Decompressed: {0}", fi.Name);
}
}
}
}
谁能帮我一下吗
提前谢谢。我觉得这句话:
string origName = curFile.Remove(curFile.Length - fi.Extension.Length);
从字符串中删除扩展名,但在创建未压缩流时使用该行
using (FileStream outFile = File.Create(origName))
相反,请使用以下命令:
using (FileStream outFile = File.Create(origName + ".txt"))
这将创建扩展名为
TXT
的未压缩文件。在我看来,这一行:
string origName = curFile.Remove(curFile.Length - fi.Extension.Length);
从字符串中删除扩展名,但在创建未压缩流时使用该行
using (FileStream outFile = File.Create(origName))
相反,请使用以下命令:
using (FileStream outFile = File.Create(origName + ".txt"))
这将创建扩展名为
TXT
的未压缩文件。这是因为在压缩文件时删除了扩展名,使其成为20110530.zip
,而不是20110530.TXT.zip
。解压缩文件的代码使用.zip
之前的所有内容作为文件名,因此自然会使用20110530
而不是20110530.txt
,这是因为在压缩文件时删除扩展名,使其成为20110530.zip
而不是20110530.txt
。解压文件的代码使用.zip
之前的所有内容作为文件名,因此您自然会使用20110530
而不是20110530.txt
在解压中创建文件时,请确保包含原始文件扩展名
//Create the decompressed file.
using (FileStream outFile = File.Create(origName)) // <<<-- here
//创建解压缩文件。
使用(FileStream outFile=File.Create(origName))/确保在解压缩中创建文件时包含原始文件扩展名
//Create the decompressed file.
using (FileStream outFile = File.Create(origName)) // <<<-- here
//创建解压缩文件。
使用(FileStream outFile=File.Create(origName))//显然,您没有编写此代码。你看过解压代码中的注释了吗?它正在变得更好,但你应该在现有问题中添加代码,而不是发布新问题。你已经问了3次相同的问题。请停止!我应该做什么不如果他们给我一个答案我会得到两个problems@G巴沙:那么也许答案不是最好的。另一种可能是,它们是对您所问内容的回答,而不是您打算问的内容……显然,您并没有编写此代码。你看过解压代码中的注释了吗?它正在变得更好,但你应该在现有问题中添加代码,而不是发布新问题。你已经问了3次相同的问题。请停止!我应该做什么不如果他们给我一个答案我会得到两个problems@G巴沙:那么也许答案不是最好的。另一种可能性是,它们是对您所问问题的回答,而不是您想要问的问题的回答……然而,这只是添加了扩展名.txt
,而不是原始扩展名,因此它只在原始扩展名实际上是.txt
@Guffa时才起作用-完全正确,但是我只是显示了txt
扩展名,因为这正是他特别想要的。然而,这只添加了扩展名.txt
,而不是原始扩展名,因此它只在原始扩展名实际上是.txt
@Guffa时才起作用-完全正确,但我只是显示了txt
扩展名,因为这正是他特别想要的。