如何压缩和解压缩文本文件

时间:2011-06-05 05:30:59

标签: c#

我有两个问题

我的问题是

  1. 我的文件名是20110505.txt,它将zip文件名压缩为 - > 20110505.txt.zip 但我需要将此文件20110505.txt压缩为 - > 20110505.zip only。
  2. 我正在使用这个dll

    使用System.IO.Compression;

    这是我的压缩代码,

    1)是我的文字格式

                string path = DayDestination + "\\" + txtSelectedDate.Text + ".txt";
                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(fi.FullName + ".zip"))
    
                //using (FileStream outFile = File.Create( fi.Name+ ".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()); 
                    } 
                } 
            } 
        } 
    } 
    
    1. 如果解压后我的zip文件名是20110505.zip,我希望我的文件名是20110505.txt。将zip文件解压缩到文本文件后我想在压缩后删除zip文件

        string Path2 = (string)(Application.StartupPath + "\\TEMP\\" + "\\" + name_atoz);
      
        DirectoryInfo di = new DirectoryInfo(Path2);
        FileInfo fi = new FileInfo(Path2);
        Compress(fi);
      
        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); 
      
                      } 
                  } 
              } 
          } 
      
    2. 我想要这个,因为我正在创建一个读取文本文件的项目。

      对我的问题是否有任何建议。

      提前致谢

1 个答案:

答案 0 :(得分:2)

正如Mitch在上面的评论中指出的那样,问题似乎是你的文件名用于zip文件。您包含FullName,但最后包含.txt。修剪它,你应该拥有你想要的东西。

我正在谈论的是这一行:

using (FileStream outFile = File.Create(fi.FullName + ".zip"))

解决问题的简单方法如下:

using (FileStream outFile = File.Create(System.Text.RegularExpressions.Regex.Replace(fi.FullName, ".txt$", "") + ".zip"))

要在解压缩后删除zip文件,请将Decompress方法中的以下行作为最后一行(在最外层使用语句之外):

File.Delete(fi);

您可能希望将其包装在try-catch中,但这是要运行的基本代码。这是一篇关于如何安全删除文件的文章:

http://www.dotnetperls.com/file-delete