Google 网上论坛不再支持新的 Usenet 帖子或订阅项。历史内容仍可供查看。

How to use Windows own Zip to compress file in C++

已查看 48 次
跳至第一个未读帖子

Dick.lee

未读,
2006年12月27日 09:32:422006/12/27
收件人
Hello Everyone:

Today I'm searching a method to compress a file with Window Zip tool in
my cpp project, I searched many links and found many related
information. However, there remained a question...

In this message:

http://groups.google.com/group/microsoft.public.scripting.vbscript/browse_thread/thread/8856e35111efd887/0aaca1b30b072ddc?lnk=st&q=Set+oCTF+%3D+oFileSys.CreateTextFile(MyTarget%2C+True)+&rnum=2&hl=zh-CN#0aaca1b30b072ddc

I got to know use VBScript can call zip funtion. I wonder whether I can
call zip funtion in C++ directly? If not, can I call VBScript
funtion(with input parameters and return value) in C++., and how?

I aslo try this to call VBScript but failed to excute my vbs, I don't
know where the problem is.. :(

http://www.codeproject.com/com/scriptdemo.asp

Any links or advices are appreiated. Thanks in advance.

-- This is the vbs which works in my operation system:

Option Explicit

Const ForReading = 1, ForWriting = 2, ForAppending = 8

Dim MySource, MyTarget, MyZipName, MyHex, MyBinary, i
Dim oShell, oApp, oFolder, oCTF, oFile
Dim oFileSys

MySource = "C:\testfile\test.log"
MyTarget = "d:\temp\SinkFolder.zip"

MyHex = Array(80, 75, 5, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0)

For i = 0 To UBound(MyHex)
MyBinary = MyBinary & Chr(MyHex(i))
Next

Set oShell = CreateObject("WScript.Shell")
Set oFileSys = CreateObject("Scripting.FileSystemObject")

'Create the basis of a zip file.
Set oCTF = oFileSys.CreateTextFile(MyTarget, True)
oCTF.Write MyBinary
oCTF.Close
Set oCTF = Nothing

Set oApp = CreateObject("Shell.Application")

'Copy the files to the compressed folder
'If Not MySource Is Nothing Then
oApp.NameSpace(MyTarget).CopyHere MySource
'End If

'Wait for compressing to begin, this was necessary on my machine
wScript.Sleep(5000)

'wait for lock to release
Set oFile = Nothing
On Error Resume Next
Do While (oFile Is Nothing)
'Attempt to open the file, this causes an Err 70,
Permission Denied when the file is already open
Set oFile = oFileSys.OpenTextFile(MyTarget, ForAppending,
False)
If Err.number <> 0 then
Err.Clear
wScript.Sleep 3000
End If
Loop

Set oFile=Nothing
Set oFileSys=Nothing

Paolo

未读,
2006年12月28日 06:14:122006/12/28
收件人

Dick.lee ha scritto:

Try this:

http://www.zlib.net/

You don't need vbscript...

Dick.lee

未读,
2006年12月29日 21:25:082006/12/29
收件人
Hi, Paolo:

Thank you for your advice, unfortunately, I can't open the link right
now..(might because of the Earthquake in Taiwan, damaged an undersea
cable, currently many websites couldn't be opened or very slow..)

With someone's help, I can call VBScript sucessfully now, Now there was
another question: similar as following one I searched in google:

"So, I'm working on a VBScript to run on our ...servers which will
backup ... on some log files. Part of the script uses the
"oApp.NameSpace(pTarget).CopyHere pSource" method to copy the files to
another location. The problem is, sometimes there may only be 20MB to
copy, sometimes 1.5GB. I would like the script to wait until the
CopyFolder method completes before continuing with processing, without
using a wscript.sleep() call - I don't know why my application check
wScript.Sleep(5000) as INVALID"

In Summary, so there are three questions:

1. How to get the status of a method(CopyHere), and return the result
value until the method is finished;

2. I also want the application method run in a silent mode(without a
window appearing) I use "FOF_SILENT' but it doesn't work? Also I want
without confirmation if file already exist...

oApp.NameSpace(pTarget).CopyHere pSource,
FOF_NOCONFIRMATION+FOF_SILENT

3. I aslo want to know is it safe to use Windows ZIP with VBSript in
this way? Are there some limitation(for example: file size is very
large, or there are many files...?)

Thanks in advance.

--- Following script is available:

function waitfor(secs)
dim start
start = timer()
do until timer() >= start + secs
loop
end function

Function CreateZip(pSource, pTarget)

Const ForReading = 1, ForWriting = 2, ForAppending = 8

Const FOF_NOCONFIRMATION = &H10
Const FOF_SILENT = &H4

Dim MyZipName, MyHex, MyBinary, i


Dim oShell, oApp, oFolder, oCTF, oFile
Dim oFileSys

MyHex = Array(80, 75, 5, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,


0, 0, 0, 0, 0)

For i = 0 To UBound(MyHex)
MyBinary = MyBinary & Chr(MyHex(i))
Next

Set oShell = CreateObject("WScript.Shell")
Set oFileSys = CreateObject("Scripting.FileSystemObject")

'Create the basis of a zip file.

Set oCTF = oFileSys.CreateTextFile(pTarget, True)


oCTF.Write MyBinary
oCTF.Close
Set oCTF = Nothing

Set oApp = CreateObject("Shell.Application")

'Copy the files to the compressed folder

'If Not pSource Is Nothing Then
oApp.NameSpace(pTarget).CopyHere pSource,
FOF_NOCONFIRMATION+FOF_SILENT
'End If


'Wait for compressing to begin, this was necessary on my machine

waitfor(200)
'wScript.Sleep(5000) ------ can't used in my
applistion

'wait for lock to release
Set oFile = Nothing
On Error Resume Next
Do While (oFile Is Nothing)
'Attempt to open the file, this causes an Err 70,
Permission Denied when the file is already open

Set oFile = oFileSys.OpenTextFile(pTarget, ForAppending, False)

If Err.number <> 0 then
Err.Clear
'wScript.Sleep 3000

waitfor(200)
End If
Loop

Set oFile=Nothing
Set oFileSys=Nothing

CreateZip = True

End Function

"Paolo 写道:

0 个新帖子