#2
吹水佬2023-04-19 09:48
windows 8 之后就有个 Compression API
参考:https://docs. 示例: 程序代码: DECLARE long CreateCompressor IN cabinet long,string@,long@ DECLARE long CreateDecompressor IN cabinet long,string@,long@ DECLARE long CloseCompressor IN cabinet long DECLARE long CloseDecompressor IN cabinet long DECLARE long Compress IN cabinet long,string@,long,string@,long,long@ DECLARE long Decompress IN cabinet long,string@,long,string@,long,long@ inFile = "D:\TEMP\tmp.txt" outFile = "D:\TEMP\tmp.mszip" defile = "D:\TEMP\tmpDe.txt" ? myCompressor(inFile, outFile, 0) && 压缩 ? myCompressor(outFile, deFile, 1) && 解压缩 RETURN FUNCTION myCompressor(inFile, outFile, nFlag) IF !BETWEEN(nFlag,0,1) RETURN .f. ENDIF LOCAL Handle, cData, inBuf, inSize, outBuf, outSize, nSize, ret Handle = 0 ret = IIF(nFlag==0, CreateCompressor(2,NULL,@Handle), CreateDecompressor(2,NULL,@Handle)) IF ret != 0 inBuf = FILETOSTR(inFile) inSize = LEN(inBuf) outSize = 0 IIF(nFlag==0, Compress(Handle,@inBuf,inSize,0,0,@outSize), Decompress(Handle,@inBuf,inSize,0,0,@outSize)) outBuf = REPLICATE(0h00,outSize) nSize = 0 ret = IIF(nFlag==0, Compress(Handle,@inBuf,inSize,@outBuf,outSize,@nSize), Decompress(Handle,@inBuf,inSize,@outBuf,outSize,@nSize)) IF ret != 0 STRTOFILE(LEFT(outBuf,nSize), outFile) ENDIF IIF(nFlag==0, CloseCompressor(Handle), CloseDecompressor(Handle)) ENDIF RETURN ret != 0 ENDFUNC |
程序代码:
LOCAL lcItemId, lcItem, lcFolderSrc, lcFileBak, lcFolderTgr, lcConfirm, lc7zCmd, lcArchiveName, lcFiles
lcItemId = ALLTRIM(bak_set.item_id)
SELECT bak_set
LOCATE FOR item_id = lcItemId
*MESSAGEBOX(lcItemId)
IF FOUND()
lcItem = ALLTRIM(bak_set.item)
lcFolderSrc = ADDBS(ALLTRIM(bak_set.folder_src))
lcFileBak = ALLTRIM(bak_set.file_bak)
lcFolderTgr = ADDBS(ALLTRIM(bak_set.folder_tgr))
lcConfirm = MESSAGEBOX("是否确认备份 " + lcItem + "?", 36, "确认备份")
IF lcConfirm = 6
*通过命令行格式化时间
lcTime = '$(Get-Date -Format "yyyyMMdd-HHmmss")'
lcArchiveName = lcFolderTgr + lcItem + '-' + lcTime + '.7z'
* 获取备份文件列表
LOCAL ARRAY laFiles[1]
ALINES(laFiles, lcFileBak, ';')
lcFiles = ""
FOR EACH lcFile IN laFiles
lcFiles = lcFiles + ' "' + lcFolderSrc + ALLTRIM(lcFile) + '"'
ENDFOR
*命令
lc7zCmd = '.\7zr.exe a -t7z ' + lcArchiveName + lcFiles
= STRTOFILE(lc7zCmd,'temp.txt')
RUN &lc7zCmd
MESSAGEBOX("备份完成: " + lcItem, 64, "备份完成")
ENDIF
ELSE
MESSAGEBOX("未找到指定的记录。", 48, "错误")
ENDIF
lcItemId = ALLTRIM(bak_set.item_id)
SELECT bak_set
LOCATE FOR item_id = lcItemId
*MESSAGEBOX(lcItemId)
IF FOUND()
lcItem = ALLTRIM(bak_set.item)
lcFolderSrc = ADDBS(ALLTRIM(bak_set.folder_src))
lcFileBak = ALLTRIM(bak_set.file_bak)
lcFolderTgr = ADDBS(ALLTRIM(bak_set.folder_tgr))
lcConfirm = MESSAGEBOX("是否确认备份 " + lcItem + "?", 36, "确认备份")
IF lcConfirm = 6
*通过命令行格式化时间
lcTime = '$(Get-Date -Format "yyyyMMdd-HHmmss")'
lcArchiveName = lcFolderTgr + lcItem + '-' + lcTime + '.7z'
* 获取备份文件列表
LOCAL ARRAY laFiles[1]
ALINES(laFiles, lcFileBak, ';')
lcFiles = ""
FOR EACH lcFile IN laFiles
lcFiles = lcFiles + ' "' + lcFolderSrc + ALLTRIM(lcFile) + '"'
ENDFOR
*命令
lc7zCmd = '.\7zr.exe a -t7z ' + lcArchiveName + lcFiles
= STRTOFILE(lc7zCmd,'temp.txt')
RUN &lc7zCmd
MESSAGEBOX("备份完成: " + lcItem, 64, "备份完成")
ENDIF
ELSE
MESSAGEBOX("未找到指定的记录。", 48, "错误")
ENDIF
代码是参考了 论坛里@liuxingang28 大佬的备份程序的代码,这里改为运行7zr.exe
运行后temp.txt中
.\7zr.exe a -t7z D:\DOCUMENT\待办-紧急\系统设计\BAK\BACKUP\测试测试-$(Get-Date -Format "yyyyMMdd-HHmmss").7z "D:\DOCUMENT\待办-紧急\系统设计\DATA\TEST.DBF" "D:\DOCUMENT\待办-紧急\系统设计\DATA\USER.DBF"
该命令能正确创建压缩文件
想请教各位大佬