繁体   English   中英

将文件从Zip保存到特定文件夹

[英]Save file from Zip into a specific folder

我正在做这个项目,我从net下载了zip文件,然后将以编程方式将其解压缩,然后将解压缩文件保存到特定的文件夹中。

例如,我要下载的zip文件包含.png,.jpg,.docx,.ppt文件。

所以我想做的是将所有.png文件保存到PNG文件夹中,将.jpg文件保存到JPG文件夹中,等等。

我已经完成了下载部分并解压缩了。

现在的问题是,如何根据文件类型将解压缩文件保存到其他文件夹中?

谁能帮我。

现在,这里是我编写的代码。

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

这是解压缩文件。

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

这是解压缩文件,但保存在文件夹中。 zip文件中的所有文件。

    private void btnUnzip_Click(object sender, RoutedEventArgs e)
    {
        UnZip(@"E:\Libraries\Pictures\EWB FileDownloader.zip", @"E:\Libraries\Pictures\sample");


    }

}

}

我想将提取的内容保存在其他文件夹中。

您可以尝试类似的方法,

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM