0

I use this simple code for log files.

    private string LogFile
    {
        get
        {
            if (String.IsNullOrEmpty(this.LogFile1))
            {
                string fn = "\\log.txt";
                int count = 0;
                while (File.Exists(fn))
                {
                    fn = fn + "(" + count++ + ").txt";
                }
                this.LogFile1 = fn;
            }
            return this.LogFile1;
        }
    }

How can I move every log file into another directory ( folder ) and make it archive like .zip? This will run once per and I will have one file per day.

File moving:

public static void Move()
    {
        string path = "";
        string path2 = "";
        try
        {
            if (!File.Exists(path))
            {
                using (FileStream fs = File.Create(path)) { }
            }
            if (File.Exists(path2))
                File.Delete(path2);

            File.Move(path, path2);
        }
        catch (Exception e)
        {
            Console.WriteLine("The process failed: {0}", e.ToString());
        }
    }
Jason Paddle
  • 1,095
  • 1
  • 19
  • 37
  • `System.IO.File.Move` and to zip the file: http://stackoverflow.com/questions/940582/how-do-i-zip-a-file-in-c-using-no-3rd-party-apis – Julián Urbano Apr 21 '13 at 18:21
  • The above code will generate a wierd filename for each iteration of the loop such as : - "\\log.txt(0).txt", "\\log.txt(0).txtlog.txt(1).txt" etc.. You may want to recheck the file name generation logic – Prahalad Deshpande Apr 21 '13 at 18:26
  • @PrahaladDeshpande yeah, I know for the wired names. I'll recheck it now. It's like this because i stored all logs in one folder. And now when i will move them I can make it with normal names.. – Jason Paddle Apr 21 '13 at 18:31

3 Answers3

1

For move files, you can use the static method Move of File class. And for zip files, you can look at GZipStream or ZipArchive class.

  • The asker wants to zip and does not ask about gzip – David Heffernan Apr 21 '13 at 18:39
  • 1
    `GZipStream` compresses a single stream using GZIP. Question is about ZIP. So `ZipArchive` is what is needed. – David Heffernan Apr 21 '13 at 19:57
  • Your edit is no good. Take a look at it and notice the two references to GZipStream. Rather than listing links in separate lines, why not use inline links. – David Heffernan Apr 21 '13 at 20:07
  • Thanks again for all your sugestions! – Jhonatas Kleinkauff Apr 21 '13 at 20:15
  • I dont agree with that. I just post some classes that maybe he doesnt know yet. And can look what fit better to what he want to do. I guess that the fact he says .zip doesnt mean that he wants to work with ZipArchive class. But ok. And @David, remember that, if he is not using .NET framework 4.5, he cant use ZipArchive class. – Jhonatas Kleinkauff Apr 21 '13 at 20:19
1

If you want windows zipping. Then check this out : https://msdn.microsoft.com/en-us/library/system.io.compression.zipfile(v=vs.110).aspx

using System;
using System.IO;
using System.IO.Compression;

namespace ConsoleApplication
{
class Program
{
    static void Main(string[] args)
    {
        string startPath = @"c:\example\start";
        string zipPath = @"c:\example\result.zip";
        string extractPath = @"c:\example\extract";

        ZipFile.CreateFromDirectory(startPath, zipPath);

        ZipFile.ExtractToDirectory(zipPath, extractPath);
    }
 }
}
Peter
  • 2,043
  • 1
  • 21
  • 45
-1
  // for moving
 File.Move(SourceFile, DestinationFile); // store in dateTime directory  to move file.

//method for zip file

private static void CompressFile(string path)
           {
               FileStream sourceFile = File.OpenRead(path);
               FileStream destinationFile = File.Create(path + ".gz");

               byte[] buffer = new byte[sourceFile.Length];
               sourceFile.Read(buffer, 0, buffer.Length);

               using (GZipStream output = new GZipStream(destinationFile,
                   CompressionMode.Compress))
               {
                   Console.WriteLine("Compressing {0} to {1}.", sourceFile.Name,
                       destinationFile.Name, false);

                   output.Write(buffer, 0, buffer.Length);
               }

               // Close the files.
               sourceFile.Close();
               destinationFile.Close();
           }