开发者

How to Zip files on Windows using only what's built-in (batch script, explorer, etc)?

I am trying to write a script that I can give to user to have it automatically zip certain files and then load them onto an ftp site. Anyone know where I could find information on writing a batch file 开发者_StackOverflow中文版for auto-zipping files using only what is available to a user running Windows?


It is possible to work with zip files by using the compressed folder (COM) support and Windows Scripting Host. You would start by creating the Shell.Application object. The FolderItem object has methods like CopyHere etc that will allow you to manipulate a zip file like a folder. See this page for zip and unzip sample code.


Since (AFAIK) Windows doesn't come with a program that can zip up files from the command line. For that matter, it doesn't come with a scriptable ftp program, either (there is an ftp command, but it's interactive).

Your best bet is to write a program in some compiled language with access to libraries for zipping files and ftp.


Multiple forums (I can't find an official Microsoft answer) suggest that the zip functionality built into Windows is provided by dlls for which there is no command line argument. For example:

The unzipping is a function of zipfldr.dll, so would use regsvr32.exe to invoke it, and as far as I know there are no arguements you can add to it for unzipping via batch file.

-- from technologyquestions.com

I would recommend a third party program, of which there are many (which you could provide along with a script): 7zip, winzip, pkzip, etc.

Also you might want to look into VBScript or WSH as alternatives to batch files.


Windows has its own zip command. You can put the code below in a backup.bat file and run.

@echo off
:: variables
set drive=E:\backup
set backupcmd=xcopy /s /c /d /e /h /i /r /y

echo ### Backing up My Documents...
%backupcmd% "e:\Repositories" "%drive%\SVNrepository\Repositories"



e:
cd "%drive%\SVNrepository"

zip -r -S -T -$ -m Repositories.zip Repositories


set filename="..\Repository_bak_%date:~0,4%%date:~5,2%%date:~8,2%_%time:~0,2%%time:~3,2%.zip"
move .\*.zip %filename%
cd E:\backup\

echo Backup Complete!
rem @pause


By csgnrg@yahoo.it

If you want convert a single file into a single zip file you can use gzip.exe

Let oProcess.Cwd = oOutput.PATH
    oProcess.SetUsePosition
    oProcess.SetUseSize 0, 0
    oProcess.ExeCmd "gzip.exe gzip -f -q -c -9 " & oSource.Source & " > " & oOutput.Source, True
    If oProcess.Error Then
       Let ErrorMsg = oProcess.ErrorMsg
       Exit Sub
    End If


A better alternative to batch file would be to use a JAR and use Java's java.util.zip package to zip the files. Or how about distributing the bat file with a JAR. People could execute the BAT file and it in turn could run the JAR.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜