From 2b99bad89d8684a63fd37533606ef3d0fdfc4dd9 Mon Sep 17 00:00:00 2001 From: wp_xxyyzz Date: Wed, 2 Sep 2015 09:36:35 +0000 Subject: [PATCH] fpspreadsheet: Write unzipped xml files of xlsx and ods to a unique temp directory. git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@4310 8e941d3f-bd1b-0410-a28a-d453659cc2b4 --- components/fpspreadsheet/fpsopendocument.pas | 3 ++- components/fpspreadsheet/fpsutils.pas | 27 +++++++++++++++++++- components/fpspreadsheet/xlsxooxml.pas | 3 ++- 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/components/fpspreadsheet/fpsopendocument.pas b/components/fpspreadsheet/fpsopendocument.pas index 45f4461ab..8fc8d233e 100755 --- a/components/fpspreadsheet/fpsopendocument.pas +++ b/components/fpspreadsheet/fpsopendocument.pas @@ -2054,7 +2054,7 @@ var sheet: TsWorksheet; begin //unzip files into AFileName path - FilePath := GetTempDir(false); + FilePath := GetUniqueTempDir(false); UnZip := TUnZipper.Create; FileList := TStringList.Create; try @@ -2155,6 +2155,7 @@ begin FWorkbook.SelectWorksheet(sheet); finally + RemoveDir(FilePath); if Assigned(Doc) then Doc.Free; end; end; diff --git a/components/fpspreadsheet/fpsutils.pas b/components/fpspreadsheet/fpsutils.pas index 01f0c13f5..52c91889d 100644 --- a/components/fpspreadsheet/fpsutils.pas +++ b/components/fpspreadsheet/fpsutils.pas @@ -150,6 +150,8 @@ function HasFormula(ACell: PCell): Boolean; function SameCellBorders(AFormat1, AFormat2: PsCellFormat): Boolean; function SameFont(AFont1, AFont2: TsFont): Boolean; +function GetUniqueTempDir(Global: Boolean): String; + procedure AppendToStream(AStream: TStream; const AString: String); inline; overload; procedure AppendToStream(AStream: TStream; const AString1, AString2: String); inline; overload; procedure AppendToStream(AStream: TStream; const AString1, AString2, AString3: String); inline; overload; @@ -170,7 +172,7 @@ var implementation uses - Math, lazutf8, fpsStrings; + Math, lazutf8, lazfileutils, fpsStrings; {******************************************************************************} { Endianess helper functions } @@ -1934,6 +1936,29 @@ begin Result := true; end; +{@@ ---------------------------------------------------------------------------- + Constructs a string of length "Len" containing random uppercase characters +-------------------------------------------------------------------------------} +function GetRandomString(Len: Integer): String; +begin + Result := ''; + While Length(Result) < Len do + Result := Result + char(ord('A') + random(26)); +end; + +{@@ ---------------------------------------------------------------------------- + Constructs a unique folder name in the temp directory of the OS +-------------------------------------------------------------------------------} +function GetUniqueTempDir(Global: Boolean): String; +var + tempdir: String; +begin + tempdir := AppendPathDelim(GetTempDir(Global)); + repeat + Result := tempdir + AppendPathDelim(GetRandomString(8)); + until not DirectoryExists(Result); +end; + {@@ ---------------------------------------------------------------------------- Appends a string to a stream diff --git a/components/fpspreadsheet/xlsxooxml.pas b/components/fpspreadsheet/xlsxooxml.pas index 71d8b9404..82827267d 100755 --- a/components/fpspreadsheet/xlsxooxml.pas +++ b/components/fpspreadsheet/xlsxooxml.pas @@ -1902,7 +1902,7 @@ var actSheetIndex: Integer; begin //unzip "content.xml" of "AFileName" to folder "FilePath" - FilePath := GetTempDir(false); + FilePath := GetUniqueTempDir(false); UnZip := TUnZipper.Create; FileList := TStringList.Create; try @@ -2029,6 +2029,7 @@ begin end; // for finally + RemoveDir(FilePath); SheetList.Free; FreeAndNil(Doc); end;