Reputation: 1674
I've created a file extractor, now it does work, however it also moves all the files from the startDir
to the destDir
along with, the zip file. How can I get this program to only move the zip file, instead of all the files?
Source:
using System;
using System.IO.Compression;
namespace ArchiveCreator
{
class Program
{
//When program is run successfully this will be the output
public string Success(string input)
{
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine(input);
return input;
}
//When program encounters an error this will be the output
public string Warn(string input)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(input);
return input;
}
//When program has information to show, this will be the output
public string Say(string input)
{
Console.ForegroundColor = ConsoleColor.DarkCyan;
Console.WriteLine(input);
return input;
}
//Main method
static void Main(string[] args)
{
//These variables are used to create a
//random string that will be used as the
//zip files name
var chars = "abcdefghijklmnopqrstuvwyxzABCDEFGHIJKLMNOPQRSTUVWXYZ";
var stringChars = new char[8];
var random = new Random();
//Info is used as provide the type of
//information that will be displayed
//by the program
Program info = new Program();
//Create the zip file name
for (int i = 0; i < stringChars.Length; i++)
{
stringChars[i] = chars[random.Next(chars.Length)];
}
string finalString = new String(stringChars);
info.Say("Starting file extraction..");
string startDir = @"c:/users/thomas_j_perkins/test_folder";
string destDir = @"c:/users/thomas_j_perkins/archive/";
string zipName = $"c:/users/thomas_j_perkins/archive/{finalString}.zip";
try
{
ZipFile.CreateFromDirectory(startDir, zipName);
ZipFile.ExtractToDirectory(zipName, destDir);
}
catch (Exception e)
{
info.Warn($"Error: {e}");
}
info.Success($"Extracted files successfully to: {destDir}");
info.Say("Press enter to exit..");
Console.ReadLine();
}
}
}
Image of directory after program is run:
Upvotes: 0
Views: 39
Reputation: 4341
Your code is creating a zip file in the destination directory when you call
ZipFile.CreateFromDirectory(startDir, zipName);
The zipname path is in the destDir. Did you mean to put it in the startDir?
string startDir = @"c:/users/thomas_j_perkins/test_folder";
string destDir = @"c:/users/thomas_j_perkins/archive/";
string zipName = $"c:/users/thomas_j_perkins/archive/{finalString}.zip";
Upvotes: 1