mrminer mrminer - 24 days ago 7
ASP.NET (C#) Question

ASP.NET Windows Console Files + filename to ZIP

I need to zip files from folder X to folder Y. When the files from folder X are zipped into folder Y. The files in folder X needs to be removed. The zip name must be the name of the file with the .DBS in that folder.

So I need to read whats the file name of the .DBS file is. Then I need to zip all the files in folder X to folder Y with the name: "Filename" (this is the same as the .DBS file) If the files are zipped and well in folder Y they need to be removed from folder X.

The code that I got at the moment will move the files of folder X too Y. So this is a start. My question is how can I get the name of the file too be the zip folder name.

Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.IO.Compression;

namespace Ramasoftzipper
{
class Program
{
static void Main(string[] args)
{


string fileName = @"160001.DBS";
string sourcePath = @"C:\RMExport";
string targetPath = @"C:\Exportrm";
string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
string destFile = System.IO.Path.Combine(targetPath, fileName);
string startPath = @"C:\RMExport";
string zipPath = (fileName);
string extractPath = @"C:\Exportrm";


if (!System.IO.Directory.Exists(targetPath))
{
System.IO.Directory.CreateDirectory(targetPath);
}


System.IO.File.Copy(sourceFile, destFile, true);


if (System.IO.Directory.Exists(sourcePath))
{
string[] files = System.IO.Directory.GetFiles(sourcePath);


foreach (string s in files)
{

fileName = System.IO.Path.GetFileName(s);
destFile = System.IO.Path.Combine(targetPath, fileName);
System.IO.File.Copy(s, destFile, true);
ZipFile.CreateFromDirectory(startPath, zipPath);
ZipFile.ExtractToDirectory(zipPath, extractPath);
}
}
else
{
Console.WriteLine("Source path does not exist!");



}
}
}
}


Thanks in advance.

Answer

(only explaining this much because I know you and you need to learn this stuff, yo :P)

you could try something like this, read comments in code for more info, this code is only showing you how to zip all files in a folder, try the next step of adding certain extentions yourself

//files to zip, you can also use the same method as above to let the user determine what path to zip
string path = @"C:\Users\WsLocal.NL-ROE2-W297\Pictures";            
string zipPath = @"C:\Users\WsLocal.NL-ROE2-W297\Desktop\zip\result.zip";            

//zip files
ZipFile.CreateFromDirectory(path, zipPath);
string[] files = Directory.GetFiles(path);

//some debugging
foreach (string filePath in files)
{
    Console.WriteLine(filePath);
}
//wait untill user presses enter
Console.ReadLine();

[EDIT]

setting the name of the zip file to a file name:

replace

string zipPath = @"C:\Users\WsLocal.NL-ROE2-W297\Desktop\zip\result.zip";

with

string[] files = Directory.GetFiles(path);
string zipName = files[0].Replace(path, "");

int index = zipName.IndexOf(".");
if (index > 0)
     zipName = zipName.Substring(0, index);

 string zipPath = @"C:\Users\WsLocal.NL-ROE2-W297\Desktop\zip\"+ zipName + ".zip";   

and delete

string[] files = Directory.GetFiles(path);

under

//zip files
ZipFile.CreateFromDirectory(path, zipPath);