Reputation: 937
I'm doing this project which I download I zip file from net, then I'll unzip it programatically then save the unzip file to a specific folder.
For example a zip file that I'm about to download contains .png, .jpg, .docx, .ppt files.
So what I'm trying to do is to save all the .png into the PNG folder, .jpg into JPG folder etc.
I've done doing the downloading part and unzipping.
The question now is how can I save unzip files into different folder according to their file type?
Can anyone help me.
As for now here is the code that i've made.
using System;
using Microsoft.Office.Interop.Excel;
using Excel = Microsoft.Office.Interop.Excel;
using System.Reflection;
using System.Net;
using System.ComponentModel;
namespace UnzipFile
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : System.Windows.Window
{
public MainWindow()
{
InitializeComponent();
}
here is to unzip the file.
public static void UnZip(string zipFile, string folderPath)
{
if (!File.Exists(zipFile))
throw new FileNotFoundException();
if (!Directory.Exists(folderPath))
Directory.CreateDirectory(folderPath);
Shell32.Shell objShell = new Shell32.Shell();
Shell32.Folder destinationFolder = objShell.NameSpace(folderPath);
Shell32.Folder sourceFile = objShell.NameSpace(zipFile);
foreach (var file in sourceFile.Items())
{
destinationFolder.CopyHere(file, 4 | 16);
}
}
here is unzip the file but saved in a folder. All the file inside the zip file.
private void btnUnzip_Click(object sender, RoutedEventArgs e)
{
UnZip(@"E:\Libraries\Pictures\EWB FileDownloader.zip", @"E:\Libraries\Pictures\sample");
}
}
}
I want to save in different folder what I've extracted.
Upvotes: 0
Views: 1896
Reputation: 8902
You could try something like,
string[] files = Directory.GetFiles("Unzip folder path");
foreach (var file in files)
{
string fileExtension = Path.GetExtension(file);
switch (fileExtension)
{
case ".jpg": File.Move(file, Path.Combine(@"Destination Folder\JPG", Path.GetFileName(file)));
break;
case ".png": File.Move(file, Path.Combine(@"Destination Folder\PNG", Path.GetFileName(file)));
break;
case ".docx": File.Move(file, Path.Combine(@"Destination Folder\DOC", Path.GetFileName(file)));
break;
case ".ppt": File.Move(file, Path.Combine(@"Destination Folder\PPT", Path.GetFileName(file)));
break;
default:
break;
}
}
Upvotes: 1