c# Shell32的zip / unzip代码,CopyHere不工作

本文关键字:CopyHere 工作 代码 unzip Shell32 zip | 更新日期: 2023-09-27 18:04:53

我对Shell32 CopyHere方法有一些问题。

首先,我从这个页面工作:http://www.codeproject.com/Tips/257193/Easily-zip-unzip-files-using-Windows-Shell32

我希望在c#中重写类似的东西,因为我几乎从未在VB.NET中工作过。

我传递一个输入目录I,其中有一个文本文件作为我的测试。在底部,我看到它实际上抓取了我的文件,我写了input.Items()的计数,结果是1,所以我认为它看到了我的目录和其中的一个文本文件。然而,虽然我的代码创建空的zip文件很好,并且至少从控制台输出看起来是抓取我的文件,但它实际上并没有将任何内容复制到zip文件中。没有抛出错误,就好像什么都没发生。

static void Zip(string i, string o)
{
        //Turn relative paths into full paths for Shell.NameSpace method.
        string ifp = Path.GetFullPath(i);
        string ofp = Path.GetFullPath(o);
        //Validate parameters.
        if (!(Directory.Exists(ifp)) || (Directory.GetFiles(ifp).Count() <= 0))
            throw new Exception("Input directory " + i + " was invalid.  " + i + " was either empty or doesn't exist.");
        string ext = ofp.Substring(ofp.Length - 4, 4);
        if (ext != ".zip")
            throw new Exception("Output zip directory " + o + " was invalid.  File extension was " + ext + " when it should be .zip.");
        if (File.Exists(ofp))
            throw new Exception("Output zip directory " + o + " was invalid.  Zip file already exists.");
        //The following data represents an empty zip file.
        byte[] startBuffer = { 80, 75, 5, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        //Create empty zip file.
        File.WriteAllBytes(ofp, startBuffer);
        if (!File.Exists(ofp))
            throw new Exception("Output zip directory " + o + " was unable to be created.");
        Shell sc = new Shell();
        //Folder which contains the files you want to zip.
        Folder input = sc.NameSpace(ifp);
        //Empty zip file as folder.
        Folder output = sc.NameSpace(ofp);
        //Copy the files into the empty zip file.
        output.CopyHere(input.Items(), 4);
        Console.WriteLine(input.Items().Count);
}

我如何使用Shell32方法有什么问题吗?

编辑:

很抱歉响应慢,我正在尝试使用DJ Kraze提供的一些代码。

澄清一些事情,不幸的是,我不能使用第三方工具,我是在。net 4.0。我要试着接受DJ的建议,看看我是否可以得到VB版本的工作或c#,他善意地张贴。

谢谢大家的帮助。

c# Shell32的zip / unzip代码,CopyHere不工作

这里的代码将相当于c#中的VB。. NET代码从codeproject页

using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
public class Form1
{
    public void Zip()
    {
        //1) Lets create an empty Zip File .
        //The following data represents an empty zip file .
        byte[] startBuffer = {
            80,
            75,
            5,
            6,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0
        };
        // Data for an empty zip file .
        FileIO.FileSystem.WriteAllBytes("d:''empty.zip", startBuffer, false);
        //We have successfully made the empty zip file .
        //2) Use the Shell32 to zip your files .
        // Declare new shell class
        Shell32.Shell sc = new Shell32.Shell();
        //Declare the folder which contains the files you want to zip .
        Shell32.Folder input = sc.NameSpace("D:''neededFiles");
        //Declare  your created empty zip file as folder  .
        Shell32.Folder output = sc.NameSpace("D:''empty.zip");
        //Copy the files into the empty zip file using the CopyHere command .
        output.CopyHere(input.Items, 4);
    }
    public void UnZip()
    {
        Shell32.Shell sc = new Shell32.Shell();
        //'UPDATE !!
        //Create directory in which you will unzip your files .
        IO.Directory.CreateDirectory("D:''extractedFiles");
        //Declare the folder where the files will be extracted
        Shell32.Folder output = sc.NameSpace("D:''extractedFiles");
        //Declare your input zip file as folder  .
        Shell32.Folder input = sc.NameSpace("d:''myzip.zip");
        //Extract the files from the zip file using the CopyHere command .
        output.CopyHere(input.Items, 4);
    }
}

根据Simon McKenzie对这个问题的回答,我建议使用如下两种方法:

    public static void ZipFolder(string sourceFolder, string zipFile)
    {
        if (!System.IO.Directory.Exists(sourceFolder))
            throw new ArgumentException("sourceDirectory");
        byte[] zipHeader = new byte[] { 80, 75, 5, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        using (System.IO.FileStream fs = System.IO.File.Create(zipFile))
        {
            fs.Write(zipHeader, 0, zipHeader.Length);
        }
        dynamic shellApplication = Activator.CreateInstance(Type.GetTypeFromProgID("Shell.Application"));
        dynamic source = shellApplication.NameSpace(sourceFolder);
        dynamic destination = shellApplication.NameSpace(zipFile);
        destination.CopyHere(source.Items(), 20);
    }
    public static void UnzipFile(string zipFile, string targetFolder)
    {
        if (!System.IO.Directory.Exists(targetFolder))
            System.IO.Directory.CreateDirectory(targetFolder);
        dynamic shellApplication = Activator.CreateInstance(Type.GetTypeFromProgID("Shell.Application"));
        dynamic compressedFolderContents = shellApplication.NameSpace(zipFile).Items;
        dynamic destinationFolder = shellApplication.NameSpace(targetFolder);
        destinationFolder.CopyHere(compressedFolderContents);
    }
}

如果你正在使用。net 4.5,有System.IO.Compression.ZipFile。这应该比您正在使用的要容易得多,并且可以更好地进行测试。

为什么要使用自己的压缩工具?

只需使用DotNetZip。免费,快速,简单。看到

  • 如何从zip文件中读取数据而无需解压缩整个文件
  • 如何在FSharp压缩文件?

创建一个zip存档可以像

一样简单
using (ZipFile zip = new ZipFile())
 {
   // add this map file into the "images" directory in the zip archive
   zip.AddFile( @"c:'images'personal'7440-N49th.png", "images");
   // add the report into a different directory in the archive
   zip.AddFile( "c:'Reports'2008-Regional-Sales-Report.pdf" , "files" ) ;
   zip.AddFile( "ReadMe.txt");
   zip.Save("c:MyZipFile.zip");
 }

解压缩也是同样的复杂程度。