From 54e28dd0db16d732a072cae76e69fc77a226ac73 Mon Sep 17 00:00:00 2001 From: sekelsenmat Date: Mon, 29 Aug 2011 11:59:47 +0000 Subject: [PATCH] Implements the possibility to save a TZipper to a TStream and uses that git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@1862 8e941d3f-bd1b-0410-a28a-d453659cc2b4 --- components/fpspreadsheet/fpsopendocument.pas | 2 +- components/fpspreadsheet/fpszipper.pp | 15 ++++-- components/fpspreadsheet/xlsxooxml.pas | 57 ++++++++++++++++++-- 3 files changed, 67 insertions(+), 7 deletions(-) diff --git a/components/fpspreadsheet/fpsopendocument.pas b/components/fpspreadsheet/fpsopendocument.pas index bd440621d..37ba898ec 100755 --- a/components/fpspreadsheet/fpsopendocument.pas +++ b/components/fpspreadsheet/fpsopendocument.pas @@ -28,7 +28,7 @@ interface uses Classes, SysUtils, - fpszipper, {NOTE: fpszipper is the latest zipper.pp Change to standard zipper when FPC 2.4 is released. Changed by JLJR} + fpszipper, {NOTE: fpszipper is the latest zipper.pp Change to standard zipper when FPC 2.8 is released} fpspreadsheet, xmlread, DOM, AVL_Tree, math, diff --git a/components/fpspreadsheet/fpszipper.pp b/components/fpspreadsheet/fpszipper.pp index 4224a08ff..2d49528bb 100644 --- a/components/fpspreadsheet/fpszipper.pp +++ b/components/fpspreadsheet/fpszipper.pp @@ -289,18 +289,21 @@ Type Property Entries[AIndex : Integer] : TZipFileEntry Read GetZ Write SetZ; default; end; + TZipperOutputDestination = (zodToFile, zodToStream); { TZipper } TZipper = Class(TObject) Private FEntries: TZipFileEntries; + FOutputDestination: TZipperOutputDestination; + FOutputStream: TStream; FZipping : Boolean; FBufSize : LongWord; FFileName : String; { Name of resulting Zip file } FFiles : TStrings; FInMemSize : Integer; - FOutFile : TFileStream; + FOutFile : TStream; FInFile : TStream; { I/O file variables } LocalHdr : Local_File_Header_Type; CentralHdr : Central_File_Header_Type; @@ -342,6 +345,8 @@ Type Property Files : TStrings Read FFiles; Property InMemSize : Integer Read FInMemSize Write FInMemSize; Property Entries : TZipFileEntries Read FEntries Write SetEntries; + Property OutputDestination: TZipperOutputDestination Read FOutputDestination Write FOutputDestination; + Property OutputStream: TStream Read FOutputStream Write FOutputStream; end; { TYbZipper } @@ -1045,7 +1050,10 @@ end; Procedure TZipper.OpenOutput; Begin - FOutFile:=TFileStream.Create(FFileName,fmCreate); + if FOutputDestination = zodToFile then + FOutFile:=TFileStream.Create(FFileName,fmCreate) + else + FOutFile := FOutputStream; End; @@ -1065,7 +1073,8 @@ End; Procedure TZipper.CloseOutput; Begin - FreeAndNil(FOutFile); + if FOutputDestination = zodToFile then + FreeAndNil(FOutFile); end; diff --git a/components/fpspreadsheet/xlsxooxml.pas b/components/fpspreadsheet/xlsxooxml.pas index 12a32b772..9b2990bb7 100755 --- a/components/fpspreadsheet/xlsxooxml.pas +++ b/components/fpspreadsheet/xlsxooxml.pas @@ -31,7 +31,7 @@ interface uses Classes, SysUtils, - fpszipper, {NOTE: fpszipper is the latest zipper.pp Change to standard zipper when FPC 2.4 is released } + fpszipper, {NOTE: fpszipper is the latest zipper.pp Change to standard zipper when FPC 2.8 is released } {xmlread, DOM,} AVL_Tree, fpspreadsheet; @@ -426,9 +426,60 @@ begin end; procedure TsSpreadOOXMLWriter.WriteToStream(AStream: TStream; AData: TsWorkbook); +var + FZip: TZipper; + i: Integer; begin - // Not supported at the moment - raise Exception.Create('TsSpreadOpenDocWriter.WriteToStream not supported'); + { Fill the strings with the contents of the files } + + WriteGlobalFiles(); + WriteContent(AData); + + { Write the data to streams } + + FSContentTypes := TStringStream.Create(FContentTypes); + FSRelsRels := TStringStream.Create(FRelsRels); + FSWorkbookRels := TStringStream.Create(FWorkbookRels); + FSWorkbook := TStringStream.Create(FWorkbook); + FSStyles := TStringStream.Create(FStyles); + FSSharedStrings := TStringStream.Create(FSharedStrings); + + SetLength(FSSheets, Length(FSheets)); + + for i := 0 to Length(FSheets) - 1 do + FSSheets[i] := TStringStream.Create(FSheets[i]); + + { Now compress the files } + + FZip := TZipper.Create; + try + FZip.OutputDestination:= zodToStream; + FZip.OutputStream := AStream; + + FZip.Entries.AddFileEntry(FSContentTypes, OOXML_PATH_TYPES); + FZip.Entries.AddFileEntry(FSRelsRels, OOXML_PATH_RELS_RELS); + FZip.Entries.AddFileEntry(FSWorkbookRels, OOXML_PATH_XL_RELS_RELS); + FZip.Entries.AddFileEntry(FSWorkbook, OOXML_PATH_XL_WORKBOOK); + FZip.Entries.AddFileEntry(FSStyles, OOXML_PATH_XL_STYLES); + FZip.Entries.AddFileEntry(FSSharedStrings, OOXML_PATH_XL_STRINGS); + + for i := 0 to Length(FSheets) - 1 do + FZip.Entries.AddFileEntry(FSSheets[i], OOXML_PATH_XL_WORKSHEETS + 'sheet' + IntToStr(i + 1) + '.xml'); + + FZip.ZipAllFiles; + finally + FSContentTypes.Free; + FSRelsRels.Free; + FSWorkbookRels.Free; + FSWorkbook.Free; + FSStyles.Free; + FSSharedStrings.Free; + + for i := 0 to Length(FSSheets) - 1 do + FSSheets[i].Free; + + FZip.Free; + end; end; {