Jason Paddle
Jason Paddle

Reputation: 1103

Create a file then create a zip and move it to another directory

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

Upvotes: 0

Views: 3060

Answers (3)

Peter
Peter

Reputation: 2267

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

Upvotes: 1

Arzu Türkmen
Arzu Türkmen

Reputation: 7

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

Upvotes: -1

Jhonatas Kleinkauff
Jhonatas Kleinkauff

Reputation: 966

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.

Upvotes: 1

Related Questions