簡體   English   中英

將文件夾內容復制到創建的.zip文件:'找不到文件或沒有讀取權限'

[英]Copy folder contents to a created .zip file: 'file not found or no read permissions'

我正在嘗試使用JScript從現有文件夾創建.zip文件,似乎我的copyHere函數沒有復制到.zip文件夾。 相反,我得到一個標題為“壓縮(壓縮)文件夾錯誤”的彈出框,其中包含“未找到文件或沒有讀取權限”的消息,即使我根據file.attributes屬性的值對文件具有讀/寫權限(32 )。

這是我正在使用的腳本:

//Get commman line arguments
var objArgs = WScript.Arguments;
var zipPath = objArgs(0);
var sourcePath = objArgs(1);

//Create empty ZIP file and open for adding
var fso = new ActiveXObject("Scripting.FileSystemObject");

var file = fso.CreateTextFile(zipPath, true);
// Create twenty-two byte "fingerprint" for .zip
file.write("PK");
file.write(String.fromCharCode(5));
file.write(String.fromCharCode(6));
file.write('\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0');

var objShell = new ActiveXObject("shell.application");
var zipFolder = new Object;

zipFolder = objShell.NameSpace(zipPath);

sourceItems = objShell.NameSpace(sourcePath).items();    

if (zipFolder != null)
{
    zipFolder.CopyHere(sourceItems);  
    WScript.Sleep(1000);      
}

現在CopyHere函數用於將sourcePath的內容復制到普通文件夾,但是當我嘗試創建.zip文件並將內容復制到該文件時,沒有任何反應。 關於為什么copyHere沒有將sourcePath的內容復制到.zip的任何想法?

調用此腳本的示例如下:

cscript win-zip.js C:\\desired\\zip\\file.zip C:\\path\\to\\source\\folder

並且期望的結果是file.zip已創建,現在包含源文件夾的內容。 這可能是權限問題嗎? 什么可能導致這種行為?


側面注意 ,使用vbScript和相同的命令我可以成功創建和填充.zip,所以為什么它不能使用jscript!

Set objArgs = WScript.Arguments
ZipFile = objArgs(0)
SourceFolder = objArgs(1)

' Create empty ZIP file and open for adding
CreateObject("Scripting.FileSystemObject").CreateTextFile(ZipFile, True).Write "PK" & Chr(5) & Chr(6) & String(18, vbNullChar)
Set zip = CreateObject("Shell.Application").NameSpace(ZipFile)

' Get items in source folder
Set sourceItems = CreateObject("Shell.Application").NameSpace(SourceFolder).Items

' Add all files/directories to the .zip file
zip.CopyHere(sourceItems)
WScript.Sleep 1000 'Wait for items to be copied

非常感謝任何有用的評論,謝謝!

我遇到了同樣的問題(找不到文件或沒有讀取權限',即使我根據file.attributes屬性的值對文件有讀/寫權限)。 我發現並在使用copyhere方法復制的目錄中某處抑制0長度文件時,問題就消失了。

正如Raymond所說,問題是我對我創建的file變量中的.zip文件夾有一個開放引用(它鎖定了文件夾,所以我無法將任何內容復制到它)。 解決方案是打電話

file.Close();

寫入文件后,我們可以訪問該文件將內容復制到它:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM