如何压缩和解压缩的文本文件 [英] how to compress and uncompress a text file

查看:206
本文介绍了如何压缩和解压缩的文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个问题。



我的问题是




  1. 我的文件名字是20110505.txt,它压缩zip文件的名称 - > 20110505.txt.zip
    但是我压缩该文件20110505.txt作为后需要 - > 20110505.zip仅



我使用该DLL



使用System.IO.Compression;



下面是我的压缩代码,



1)是我的文字格式

 路径字符串= DayDestination +\\+ txtSelectedDate.Text +名.txt; 
的StreamWriter Strwriter =新的StreamWriter(路径);
DirectoryInfo的DI =新DirectoryInfo的(路径);
FileInfo的网络连接=新的FileInfo(路径);
压缩(FI);

公共静态无效压缩(FileInfo的网络连接)
{
//获取源文件的流。使用
(的FileStream INFILE = fi.OpenRead())
{

//防止压缩隐藏和已压缩的文件。
如果((File.GetAttributes(fi.FullName)及FileAttributes.Hidden)
= FileAttributes.Hidden&安培;!fi.Name =.ZIP)
{
//创建压缩文件。
使用(的FileStream不过outFile = File.Create(fi.FullName +.ZIP))

//使用(的FileStream不过outFile = File.Create(fi.Name +.ZIP) )
{使用
(GZipStream压缩=新GZipStream(不过outFile,
CompressionMode.Compress))
{
//复制源文件到压缩流。
字节[]缓冲区=新的字节[4096];
INT numRead;
而((numRead = inFile.Read(缓冲液,0,buffer.Length))!= 0)
{
Compress.Write(缓冲液,0,numRead);
}
Console.WriteLine(压缩{0}从{1}到{2}字节,
fi.Name,fi.Length.ToString(),outFile.Length。的ToString());
}
}
}
}
}




  1. 如果我的zip文件的名称是20110505.zip解压我想我的20110505.txt文件名之后。 uncomressing zip文件为文本文件后,我想压缩

     字符串路径2 =(字符串)(应用程序后,删除该压缩文件。 StartupPath +\\TEMP\\+\\+ name_atoz); 

    DirectoryInfo的DI =新DirectoryInfo的(路径2);
    FileInfo的网络连接=新的FileInfo(路径2);
    压缩(FI);

    公共静态无效的解压(FileInfo的网络连接)
    {
    //获取源文件的流。使用
    (的FileStream INFILE = fi.OpenRead())
    {
    //从report.doc.gz获取原始文件扩展名,例如文档。
    串curFile = fi.FullName;
    串origName = curFile.Remove(curFile.Length - fi.Extension.Length);

    //创建压缩文件。使用
    (的FileStream不过outFile = File.Create(origName))
    {使用(GZipStream解压=新GZipStream(INFILE,
    CompressionMode.Decompress))

    $ { b $ b //减压流复制到输出文件中。
    字节[]缓冲区=新的字节[4096];
    INT numRead;
    而((numRead = Decompress.Read(缓冲液,0,buffer.Length))!= 0)
    {
    outFile.Write(缓冲液,0,numRead);
    }
    Console.WriteLine(解压缩:{0},fi.Name);

    }
    }
    }
    }




我想这是因为我创建一个项目,该项目将读取的文本文件。



有没有我的问题的任何建议。



在此先感谢


< DIV CLASS =h2_lin>解决方案

由于米奇在上面的评论中指出,这个问题似乎与正在使用的压缩文件的文件名。你包括全名,但在结尾处中的.txt。内饰方面即关,你应该有你想要的东西。



我在谈论该生产线是这个:

   

$ b>
$ b

有一个简单的方法来解决这将是如下:

 使用(的FileStream不过outFile =文件.Create(System.Text.RegularExpressions.Regex.Replace(fi.FullName,名.txt $,)+.ZIP))

要删除您的zip文件后,你解压,把你的解压方法以下行作为最后一行(外你最外面的using语句):

  File.Delete(FI); 

您可能想包装在一个try-catch,但是这是基本的代码运行。下面是关于如何安全地删除文件一篇文章:



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


I am having two problems

My problems are

  1. My file name is 20110505.txt , and it is compressing a zip file name as -> 20110505.txt.zip But I need after compressing this file 20110505.txt as --> 20110505.zip only.

I am using this dll

using System.IO.Compression;

Here is my code for compress,

1)is my text format

            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. If my zip file name is 20110505.zip after uncompressing i want my file name to be 20110505.txt . after uncomressing the zip file to text file i want to delete the zip file after compressing

      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); 
    
                    } 
                } 
            } 
        } 
    

I want this because i am creating a project which reads the text file .

Is there any suggestion for my problem.

Thanks In Advance

解决方案

As Mitch pointed out in the comments above, the problem seems to be with your file name being used for the zip file. You include the FullName, but that has the .txt at the end. Trim that off and you should have what you want.

The line I'm talking about is this one:

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

A simple way to fix it would be as follows:

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

To delete your zip file after you decompress it, put the following line in your Decompress method as the very last line (outside your outer-most using statement):

File.Delete(fi);

You probably want to wrap that in a try-catch but this is the basic code to run. Here is an article on how to delete a file safely:

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

这篇关于如何压缩和解压缩的文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆