G Basha
G Basha

Reputation: 141

how to compress and uncompress a text file

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

Upvotes: 1

Views: 4668

Answers (1)

IAmTimCorey
IAmTimCorey

Reputation: 16755

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

Upvotes: 2

Related Questions