关于 Delphi 中流的使用(10): 压缩与解压缩进度 - 回复 ilst 的问题
生活随笔
收集整理的這篇文章主要介紹了
关于 Delphi 中流的使用(10): 压缩与解压缩进度 - 回复 ilst 的问题
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
為什么80%的碼農都做不了架構師?>>> ??
問題來源: http://www.cnblogs.com/del/archive/2008/08/02/1022539.html#1275599本例是在這個例子的基礎上修改的: http://www.cnblogs.com/del/archive/2008/01/01/1022539.html
本例效果圖:
代碼文件:
unit Unit1;interfaceusesWindows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,Dialogs, StdCtrls, ComCtrls;typeTForm1 = class(TForm)Button1: TButton;Button2: TButton;ProgressBar1: TProgressBar;procedure Button1Click(Sender: TObject);procedure Button2Click(Sender: TObject);procedure CsProgress(Sender: TObject); {壓縮的 OnProgress 事件}procedure DsProgress(Sender: TObject); {解壓縮的 OnProgress 事件}end;varForm1: TForm1;implementation{$R *.dfm}uses Zlib;{壓縮的 OnProgress 事件} procedure TForm1.CsProgress(Sender: TObject); beginProgressBar1.Position := Integer(TCompressionStream(Sender).Position div 1024);Application.ProcessMessages; end;{解壓縮的 OnProgress 事件} procedure TForm1.DsProgress(Sender: TObject); beginProgressBar1.Position := Integer(TDecompressionStream(Sender).Position div 1024);Application.ProcessMessages; end;{壓縮} procedure TForm1.Button1Click(Sender: TObject); varcs: TCompressionStream;fs,ms: TMemoryStream;num: Integer; beginfs := TMemoryStream.Create;fs.LoadFromFile('c:\temp\test.txt'); {我是用一個 15M 的文本文件測試的}num := fs.Size;ms := TMemoryStream.Create;ms.Write(num, SizeOf(num));cs := TCompressionStream.Create(clMax, ms);{在原來代碼基礎是添加這兩行}ProgressBar1.Max := Integer(fs.Size div 1024);cs.OnProgress := CsProgress;fs.SaveToStream(cs);cs.Free;ms.SaveToFile('c:\temp\test.zipx');ms.Free;fs.Free; end;{解壓縮} procedure TForm1.Button2Click(Sender: TObject); vards: TDecompressionStream;fs,ms: TMemoryStream; num: Integer; beginfs := TMemoryStream.Create;fs.LoadFromFile('c:\temp\test.zipx');fs.Position := 0;fs.ReadBuffer(num,SizeOf(num));ms := TMemoryStream.Create;ms.SetSize(num);ds := TDecompressionStream.Create(fs);{在原來代碼基礎是添加這兩行}ProgressBar1.Max := Integer(ms.Size div 1024);ds.OnProgress := DsProgress;ds.Read(ms.Memory^, num);ms.SaveToFile('c:\temp\test2.txt');ds.Free;ms.Free;fs.Free; end;end. 窗體文件:
object Form1: TForm1Left = 0Top = 0Caption = 'Form1'ClientHeight = 136ClientWidth = 205Color = clBtnFaceFont.Charset = DEFAULT_CHARSETFont.Color = clWindowTextFont.Height = -11Font.Name = 'Tahoma'Font.Style = []OldCreateOrder = FalsePixelsPerInch = 96TextHeight = 13object Button1: TButtonLeft = 64Top = 24Width = 75Height = 25Caption = #21387#32553TabOrder = 0OnClick = Button1Clickendobject Button2: TButtonLeft = 64Top = 55Width = 75Height = 25Caption = #35299#21387#32553TabOrder = 1OnClick = Button2Clickendobject ProgressBar1: TProgressBarLeft = 24Top = 97Width = 150Height = 17TabOrder = 2end end
轉載于:https://my.oschina.net/hermer/blog/320097
總結
以上是生活随笔為你收集整理的关于 Delphi 中流的使用(10): 压缩与解压缩进度 - 回复 ilst 的问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何为file增加文件类型的识别
- 下一篇: 如何使用Microsoft技术栈