unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, JPEG, ExtCtrls, ExtDlgs;
type
TForm1 = class(TForm)
Button1: TButton;
ListBox1: TListBox;
Button2: TButton;
OpenDialog1: TOpenPictureDialog;
Panel1: TPanel;
Image1: TImage;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
TMyFileRec = record
FileName : ShortString; //文件名
BeginPos: LongInt; //资源开始位置
Size: LongInt; //资源结束位置
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var fsRead,fsWrite: TFileStream;
RA: array of TMyFileRec;
i,iCount: Integer;
begin
if OpenDialog1.Execute then
begin
fsWrite := TFileStream.Create(ExtractFilePath(ParamStr(0))+'MyFilePack.bin',fmCreate or fmOpenReadWrite);
ListBox1.Items := OpenDialog1.Files;
iCount := OpenDialog1.Files.Count;
SetLength(RA,iCount);
for i:=0 to iCount-1 do
begin
fsRead := TFileStream.Create(ListBox1.Items.Strings,fmOpenRead);
RA.FileName := ExtractFileName(ListBox1.Items.Strings);
if i=0 then
RA.BeginPos := 0
else
RA.BeginPos := RA[i-1].BeginPos + RA[i-1].Size;
RA.Size := fsRead.Size;
fsWrite.Seek(0,soFromEnd);
fsWrite.CopyFrom(fsRead,fsRead.Size);
fsRead.Free;
end;
for i:=0 to iCount-1 do
fsWrite.WriteBuffer(RA,SizeOf(TMyFileRec));
fsWrite.WriteBuffer(iCount,SizeOf(iCount));
fsWrite.Free;
ShowMessage('写包完成!');
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
var fsRead: TFileStream;
MS: TMemoryStream;
Buf: TMyFileRec;
RA: array of TMyFileRec;
i,iCount,InfoSz: Integer;
begin
fsRead := TFileStream.Create(ExtractFilePath(ParamStr(0))+'MyFilePack.bin',fmOpenRead);
MS := TMemoryStream.Create;
fsRead.Seek(-1*SizeOf(iCount),soFromEnd);
fsRead.ReadBuffer(iCount,SizeOf(iCount));
InfoSz := SizeOf(TMyFileRec)*iCount;
SetLength(RA,iCount);
fsRead.Seek(-(SizeOf(iCount)+InfoSz),soFromEnd);
for i:=0 to iCount-1 do
begin
fsRead.ReadBuffer(Buf, SizeOf(TMyFileRec));
RA := Buf;
end;
i := StrToInt(Edit1.Text);
if i>iCount then
begin
ShowMessage('不存在第'+IntToStr(i)+'幅图!');
fsRead.Free;
MS.Free;
Exit;
end;
fsRead.Seek(RA.BeginPos,soFromBeginning);
MS.CopyFrom(fsRead,RA.Size);
MS.SaveToFile(ExtractFilePath(ParamStr(0))+RA.FileName);
Image1.Picture.LoadFromFile(ExtractFilePath(ParamStr(0))+RA.FileName);
RA := nil;
fsRead.Free;
MS.Free;
end;
end.
备注:Image不能直接从流中读出JPEG图片进行显示,此时用一个TJpegImage来代换一下
即可,即:先用一个TJPEGImage对象读出流中的数据,然后Image1.Picture.Assign(该JPEG对象)。