首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >c# Shell32邮政编码/解压缩码,CopyHere不工作

c# Shell32邮政编码/解压缩码,CopyHere不工作
EN

Stack Overflow用户
提问于 2014-05-23 16:42:41
回答 4查看 6.9K关注 0票数 1

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

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

我希望在C#中重写类似的东西,因为我很少在VB.NET中工作。

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

代码语言:javascript
复制
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#。

谢谢大家的帮助。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2014-05-23 16:54:00

此处的代码将是与代码项目页面中的C#代码等效的VB.NET代码。

代码语言:javascript
复制
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);

    }

}
票数 5
EN

Stack Overflow用户

发布于 2014-10-08 11:38:08

基于Simon的答案回答这个问题,我建议使用以下两种方法:

代码语言:javascript
复制
    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);
    }
}
票数 1
EN

Stack Overflow用户

发布于 2014-05-23 16:57:48

如果您使用的是.NET 4.5,则有System.IO.Compression.ZipFile。这应该会容易得多--并且更好地测试您正在使用的内容。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23834472

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档