怎樣壓縮sql server2000的數據庫備份文件,像rar一樣?小弟有一7m的sql server2000
數據庫備份文件,在程序中怎樣壓縮啊?
代碼如下:
procedure TForm1.Button2Click(Sender: TObject);
var
SHExecInfo: SHELLEXECUTEINFO;
begin
SHExecInfo.cbSize := sizeof(SHELLEXECUTEINFO);
SHExecInfo.fMask := SEE_MASK_NOCLOSEPROCESS;
SHExecInfo.Wnd := Handle;
SHExecInfo.lpVerb := nil;
SHExecInfo.lpFile := 'WinRAR.exe';
SHExecInfo.lpParameters := 'a e:\qwqw.rar e:\qwqw';
SHExecInfo.lpDirectory := nil;
SHExecInfo.nShow := SW_SHOW;
SHExecInfo.hInstApp := Handle;
ShellExecuteEx(@SHExecInfo);
WaitForSingleObject(SHExecInfo.hProcess, INFINITE);
CloseHandle(SHExecInfo.hProcess);
ShellExecute(application.MainForm.Handle,'open','winrar.exe',PChar('a e:\zqzq.rar e:\zqzq'),'',SW_show);
ShowMessage('壓縮完畢!'); }
這是一段壓縮圖片的代碼,壓縮文件原理相同,只需稍做改動即可。
代碼如下:
var
mss: TMemoryStream;
zip: TDeCompressionStream;
zip1: TCompressionStream;
fs : TFileStream;
fBuf: Array[0..16383] of Byte;
flen: Integer;
//從數據庫中取出圖片
//...寫出SQL語句以取得有圖片的記錄,此處從略
mss := TMemoryStream.Create;
fs := TFileStream.Create('filename.jpg',fmCreate or fmOpenWrite);
try
TBlobField(Que.FieldByName('pic')).SaveToStream(mss);
zip := TDeCompressionStream.Create(fs);
try
flen := zip.Read(fbuf, SizeOf(fBuf));
while flen > 0 do begin
fs.Write(fbuf, flen);
flen := zip.Read(fbuf, SizeOf(fBuf));
end;
finally
FreeAndNil(zip);
end;
finally
mss.Free;
fs.Free;
end;
//將文件filename.jpg中的圖片保存到數據庫
//...寫出SQL語句,打開Que,並定位到要保存圖片的記錄,此處從略
fs := TFileStream.Create('filename.jpg',fmOpenRead);
mss := TMemoryStream.Create;
try
zip1 := TCompressionStream.Create(clDefault,mss);
try
flen := fs.Read(fbuf, SizeOf(fBuf));
while flen > 0 do begin
zip1.Write(fbuf, flen);
flen := fs.Read(fbuf, SizeOf(fBuf));
end;
//保存到數據庫
TBlobField(Que.FieldByName('pic')).LoadFromStream(mss);
Que.UpdateBatch();
//...
finally
zip1.Free;
end;
finally
fs.Free;
mss.Free;
end;