从Zip文件中删除文件夹 [英] Deleting a Folder from a Zip file

查看:496
本文介绍了从Zip文件中删除文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从Zip文件中删除文件夹.

所以我的文件结构如下:

内部优先:

我尝试在此处使用代码从Zip中删除文件作者:Siddharth Rout,但它只能移动文件,文件夹显然是空的, 但未从Zip中删除 .

代码:

Sub del()


Dim oApp As Object
Dim fl As Object
Set oApp = CreateObject("Shell.Application")

    For Each fl In oApp.Namespace("C:\Users\mohit.bansal\Desktop\Test\test\first.zip\first").Items
    'Path to a folder inside the Zip
        oApp.Namespace("C:\Users\mohit.bansal\Desktop\Test\test\Dump").MoveHere (fl.path)
    Next

End Sub

显然,它将所有文件移动到文件夹Dump,但名为Second的文件夹在Zip中保持不变.尽管第二秒钟的所有文件也都被移动了.

我可以使用命令Kill&然后RmDir删除移动的文件和文件夹.但是,如何使第二个文件夹从Zip中消失.

注意:

  • 我并不是要从Zip中移走所有文件,这只是使代码简短的测试条件.
  • 我不是在寻找一种解决方法来解压缩文件,删除文件夹并重新压缩所有内容.
  • 让我知道是否需要其他信息.

解决方案

我能够删除该文件夹.

CreateObject("Shell.Application").Namespace("C:\Users\mohit.bansal\Desktop\Test\test\first.zip\first\second").Self.Verbs.Item(4).DoIt

如GSerb所指出的,最好使用InvokeVerb)"Delete"删除文件夹.

 CreateObject("Shell.Application").Namespace("C:\Users\mohit.bansal\Desktop\Test\test\first.zip\first\second").Self.InvokeVerb ("Delete")

我无法取消文件删除确认对话框.


因此,使用.Self.Verbs.Item(4),我们可以访问从0开始的右键单击选项.

演示:

附录

我最后的工作解决方案是将Xip文件的内容复制到临时文件夹,删除子文件夹,删除原始zip文件,创建新的zip文件,然后将其余项目复制到新的zip文件. /p>

用法:

  DeleteZipSubDirectory "E:\first.zip","\first\second"   

Sub DeleteZipSubDirectory(ZipFile As Variant, SubFolderRelativePath As Variant)
    Dim tempPath As Variant

    'Make Temporary Folder
    tempPath = Environ("Temp") & "\"
    Do While Len(Dir(tempPath, vbDirectory)) > 0
        tempPath = tempPath & "0"
    Loop
    MkDir tempPath

    Dim control As Object
    Set control = CreateObject("Shell.Application")
    'Copy Zip Contents to Temporary Folder
    control.Namespace(tempPath).CopyHere control.Namespace(ZipFile).Items

    'Debug.Print tempPath

    With CreateObject("Scripting.FileSystemObject")
        'Delete Target Folder
        .DeleteFolder tempPath & SubFolderRelativePath
        'Delete Original FIle
        Kill ZipFile

        'First we create an empty zip file: https://www.exceltrainingvideos.com/zip-files-using-vba/
        Open ZipFile For Output As #1
        Print #1, Chr$(80) & Chr$(75) & Chr$(5) & Chr$(6) & String(18, 0)
        Close #1

        'Copy the Remaining Items into the new Zip File
        control.Namespace(ZipFile).CopyHere control.Namespace(tempPath).Items
        Application.Wait Now + TimeValue("0:00:02")
        'Delete Temporary Folder
        .DeleteFolder tempPath
    End With
End Sub

感谢Mikku和SiddharthRout的帮助.

I am trying to Delete a Folder from the Zip file.

So My file structure is like:

Inside First:

I tried to use the code here Deleting Files from A Zip By Siddharth Rout, But it only Moves the files, apparently the folder becomes empty, but isn't deleted from the Zip.

Code:

Sub del()


Dim oApp As Object
Dim fl As Object
Set oApp = CreateObject("Shell.Application")

    For Each fl In oApp.Namespace("C:\Users\mohit.bansal\Desktop\Test\test\first.zip\first").Items
    'Path to a folder inside the Zip
        oApp.Namespace("C:\Users\mohit.bansal\Desktop\Test\test\Dump").MoveHere (fl.path)
    Next

End Sub

Apparently it moves all the files to the folder Dump, but the folder named Second stays intact in the Zip. Though all the files from second are also moved.

I can use the command Kill & RmDir afterwards to delete the moved files and Folder. But how to make the Second Folder Vanish from Zip.

Note:

  • I don't mean to Move all the files from Zip, It's just the testing condition to keep the code short.
  • I am not looking for a Workaround to Unzip the file, delete the folder and Re-zip everything.
  • Let me know if any other information is Required.

解决方案

I was able to delete the folder.

CreateObject("Shell.Application").Namespace("C:\Users\mohit.bansal\Desktop\Test\test\first.zip\first\second").Self.Verbs.Item(4).DoIt

As GSerb pointed out it may be better to use InvokeVerb)"Delete" to delete the folder.

 CreateObject("Shell.Application").Namespace("C:\Users\mohit.bansal\Desktop\Test\test\first.zip\first\second").Self.InvokeVerb ("Delete")

I have not been able to suppress the file deletion conformation dialog.


So using .Self.Verbs.Item(4) we can access the Right Click Options starting with 0.

Demo:

Addendum

My final working solution was to copy the contents of the Xip file to a temp folder, delete the sub folder, delete the original zip file, create a new zip file, and copy the remaining items to the new zip file.

Usage:

  DeleteZipSubDirectory "E:\first.zip","\first\second"   

Sub DeleteZipSubDirectory(ZipFile As Variant, SubFolderRelativePath As Variant)
    Dim tempPath As Variant

    'Make Temporary Folder
    tempPath = Environ("Temp") & "\"
    Do While Len(Dir(tempPath, vbDirectory)) > 0
        tempPath = tempPath & "0"
    Loop
    MkDir tempPath

    Dim control As Object
    Set control = CreateObject("Shell.Application")
    'Copy Zip Contents to Temporary Folder
    control.Namespace(tempPath).CopyHere control.Namespace(ZipFile).Items

    'Debug.Print tempPath

    With CreateObject("Scripting.FileSystemObject")
        'Delete Target Folder
        .DeleteFolder tempPath & SubFolderRelativePath
        'Delete Original FIle
        Kill ZipFile

        'First we create an empty zip file: https://www.exceltrainingvideos.com/zip-files-using-vba/
        Open ZipFile For Output As #1
        Print #1, Chr$(80) & Chr$(75) & Chr$(5) & Chr$(6) & String(18, 0)
        Close #1

        'Copy the Remaining Items into the new Zip File
        control.Namespace(ZipFile).CopyHere control.Namespace(tempPath).Items
        Application.Wait Now + TimeValue("0:00:02")
        'Delete Temporary Folder
        .DeleteFolder tempPath
    End With
End Sub

Thanks for the Mikku and SiddharthRout for there help.

这篇关于从Zip文件中删除文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆