如何将zip文件解压缩为文本扩展名

时间:2011-06-05 13:13:36

标签: c#

  

可能重复:
  how to read a *.file
  how to compress and uncompress a text file

您好,

我在将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);

                }
            }
        }
    } 

任何人都可以帮助我,

提前致谢。

3 个答案:

答案 0 :(得分:3)

我觉得这一行:

string origName = curFile.Remove(curFile.Length - fi.Extension.Length);

从字符串中删除扩展名,但在创建未压缩的流时使用该行。

using (FileStream outFile = File.Create(origName))

相反,请使用:

using (FileStream outFile = File.Create(origName + ".txt"))

这应创建一个扩展名为TXT的未压缩文件。

答案 1 :(得分:1)

这是因为您在压缩文件时删除了扩展程序,使其变为20110530.zip而不是20110530.txt.zip。解压缩文件的代码使用.zip之前的所有内容作为文件名,因此您最终会使用20110530而不是20110530.txt

答案 2 :(得分:1)

确保在解压缩

中创建文件时包含原始文件扩展名
//Create the decompressed file. 
using (FileStream outFile = File.Create(origName)) // <<<-- here