使用Zlib解壓縮不完整,該怎麼解決? |
答題得分者是:yowlc
|
Reiji
初階會員 ![]() ![]() 發表:30 回覆:57 積分:32 註冊:2008-06-26 發送簡訊給我 |
小弟試著網路上找來的範例寫了一段壓縮和解壓縮的程式
使用光碟中的Zlib 壓縮過程看似沒有問題 但解壓縮就問題很大了 例如壓縮一個bmp檔,解壓縮後根本打不開 壓縮一個jpg檔,解壓縮後圖片打開有45%是灰色的(被吃掉了 ![]() 壓縮文件後解壓縮會打不開... 仔細查看解壓縮後檔案,大小和壓縮後檔案一樣大?! 例如我壓縮1.bmp(1MB),壓縮後1.pk(1KB),解壓縮另存為1-2.bmp( use zlib; procedure TForm1.PackStream(const Src: TStream; Dst: TStream); var CompStream: TCompressionStream; begin Assert(Src<>Nil); Assert(Dst<>Nil); CompStream := TCompressionStream.Create(clDefault,Dst); try Src.Seek(0,soFromBeginning); CompStream.CopyFrom(Src,0); finally CompStream.Free; end; end; procedure TForm1.test; var SrcStream,PackedStream,UnpackedStream:TMemoryStream; begin SrcStream:=TMemoryStream.Create; Try SrcStream.LoadFromFile('c:\1.bmp'); PackedStream:=TMemoryStream.Create; try PackStream(SrcStream,PackedStream); PackedStream.Seek(0,soFromBeginning); PackedStream.saveToFile('c:\1.pk'); UnpackedStream:=TMemoryStream.Create; try UnpackStream(PackedStream,UnpackedStream); UnpackedStream.Seek(0,soFromBeginning); UnpackedStream.SaveToFile('c:\1-2.bmp'); finally UnpackedStream.Free; end; finally PackedStream.Free; end; finally SrcStream.Free; end; end; procedure TForm1.UnpackStream(const Src: TStream; Dst: TStream); var DecompStream: TDecompressionStream; NewSize:integer; begin Assert(Src<>Nil); Assert(Dst<>Nil); DecompStream := TDecompressionStream.Create(Src); try //NewSize:=Src.Seek(0,soFromEnd); NewSize:=Src.Size; Src.Seek(0,soFromBeginning); NewSize:=Dst.CopyFrom(DecompStream,NewSize); Dst.Size:=NewSize; finally DecompStream.Free; end; end; procedure TForm1.Button1Click(Sender: TObject); begin test; end;[/code]
------
永遠都是新手 |
yowlc
一般會員 ![]() ![]() 發表:1 回覆:5 積分:6 註冊:2005-04-24 發送簡訊給我 |
試試看可不可以 [code delphi] .... .... uses ZLib, Classes, StrUtils, SysUtils; ..... ..... {================================================================= 功能: 壓縮串流類資料 參數: InStream: 要壓縮的串流資料; OutStream: 壓縮後的串流資料 CompressLevel: 壓縮程度 1: 沒壓縮 2: 快速壓縮 3: 一般壓縮 4: 最大壓縮 返回: 成功: True; 失敗: False 備注: 版本: =================================================================} Function CompressStream(InStream, OutStream: TStream; CompressLevel: Byte = 3) : Boolean; var cps : TCompressionStream; clv : TCompressionLevel; begin Result := False; case CompressLevel of 1 : clv := clNone; 2 : clv := clFastest; 3 : clv := clDefault; 4 : clv := clMax; else clv := clDefault; end; InStream.Position := 0; try cps := TCompressionStream.Create(clv, OutStream); try cps.CopyFrom(InStream, 0); OutStream.Position := 0; Result := True; finally cps.Free; end; except end; end; {================================================================= 功能: 解壓縮串流類資料 參數: InStream: 要解壓縮的串流資料; OutStream: 解壓縮後的串流資料 返回: 成功: True; 失敗: False 備注: 版本: =================================================================} Function DeCompressStream(InStream, OutStream: TStream) : Boolean; var cps : TDeCompressionStream; Buf : Array[1..2048] of Byte; iByteRead : Integer; begin Result := False; InStream.Position := 0; try cps := TDeCompressionStream.Create(InStream); try Repeat iByteRead := cps.Read(Buf, Sizeof(Buf)); if iByteRead > 0 then OutStream.Write(Buf, iByteRead); Until (iByteRead = 0); OutStream.Position := 0; Result := True; finally cps.Free; end; except end; end; procedure TForm1.Button01Click(Sender: TObject); var InStream, OutStream : TMemoryStream; begin // 檔案壓縮 InStream := TMemoryStream.Create; OutStream := TMemoryStream.Create; try InStream.LoadFromFile('C:\org.bmp'); CompressStream(InStream, OutStream); OutStream.SaveToFile('c:\org-pak.pak'); InStream.Clear; OutStream.Clear; InStream.LoadFromFile('C:\org-pak.pak'); DeCompressStream(InStream, OutStream); OutStream.SaveToFile('c:\org-upak.bmp'); finally OutStream.Free; InStream.Free; end; end; [/code] |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |