Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
596 views
in Technique[技术] by (71.8m points)

windows - Copy folder contents to a created .zip file: 'file not found or no read permissions'

I'm trying to create a .zip file from an existing folder using JScript and it seems that my copyHere function is not copying to the .zip folder. Instead I get a popup box titled 'Compressed (zipped) Folder Error' with the message 'file not found or no read permissions' even though I have read/write privileges on the file according to the value of my file.attributes property (32).

Here is the script I'm using:

//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('');

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

Now the CopyHere function works for copying the contents of the sourcePath to a normal folder but when I try to create a .zip file and copy the contents to that, nothing happens. Any ideas on why copyHere is not copying the contents of the sourcePath to the .zip?

An Example for calling this script would be:

cscript win-zip.js C:desiredzipfile.zip C:pathosourcefolder

And the desired outcome would be that file.zip was created and now contains the contents of the source folder. Could this be a problem with permissions? What might cause this behavior?


Side Note, using a vbScript and the same commands I can successfully create and populate a .zip, so why doesn't it work using 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

Any helpful comments are greatly appreciated, thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I encountered the same problem (file not found or no read permissions' even though I have read/write privileges on the file according to the value of my file.attributes property). The problem disapeared as soon as I found and suppress a 0 length file somewhere in the directory to be copied with the copyhere method.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...