ONLINE HELP
 WINDEVWEBDEV AND WINDEV MOBILE

Help / WLanguage / WLanguage functions / Standard functions / Archive functions
  • Example 1: Finding a file whose path contains the "Temp" character string in a archive
  • Example 2: Finding a file of a given type in an archive
  • Example 3: Finding a file of a given type in an xlsx archive
zipFindFile (Example)
Example 1: Finding a file whose path contains the "Temp" character string in a archive
WINDEVWEBDEV - Server codeReports and QueriesUser code (UMC)Ajax The following code is used to find a file in an archive. The search is a generic search: the path of the files found contain "Temp". Each file found will be displayed in a table. The number of files whose path contains the word "Temp" is counted.
// Declare and initialize the variables
ArchiveName is string = "MyArchive"
SoughtFile is string = "Temp"
AListOfFiles is string
ExtractFile is string
ResFileFound is int = 1
NbFiles is int
Index is int = 1
Counter is int = 0
 
// List the files found in the archive
AListOfFiles = zipListFile(ArchiveName)
 
// Number of files found in the archive
NbFiles = zipNbFile(ArchiveName)
 
FOR Subscript TO NbFiles
// Find the file in an archive (generic search)
ResFileFound = zipFindFile(ArchiveName, SoughtFile, False)
// Extract the path of the file found
ExtractFile = ExtractString(AListOfFiles, ResFileFound, CR)
// Display the file found in a table
TableAdd(TABLE_FileTable, ExtractFile)
// Count the number of file whose path contains "Temp"
Counter = Counter + 1
END
 
// Display the number of files whose path contains "Temp"
Info("Number of files whose path contains Temp: ", Counter)
Example 2: Finding a file of a given type in an archive
WINDEVWEBDEV - Server codeReports and QueriesUser code (UMC)Ajax The following code is used to find a ".DOC" file in an archive. Each file found will be displayed in a memory table (named "TABLE_ArchiveTable").
// Declare and initialize the variables
ArchiveName is string = "MyArchive"
SoughtFile is string = ".DOC"
AListOfFiles is string
ExtractFile is string
ResFileFound is int = 1
NbFiles is int
Index is int = 1
 
// List the files found in the archive
AListOfFiles = zipListFile(ArchiveName)
 
// Number of files found in the archive
NbFiles = zipNbFile(ArchiveName)
 
FOR Subscript TO NbFiles
// Find the file in an archive (generic search)
ResFileFound = zipFindFile(ArchiveName, SoughtFile, False)
IF zipExtractPath(ArchiveName, ResFileFound, zipExtension) = ".DOC" THEN
 // Extract the path of the file found
 ExtractFile = ExtractString(AListOfFiles, ResFileFound, CR)
 // Display the file found in a table
 TableAdd(TABLE_ArchiveTable, ExtractFile)
END
END
Example 3: Finding a file of a given type in an xlsx archive
This code is used to find a file in an xlsx archive, to modify it and to replace it in the archive.
// Physical name of the file (the xlsx files are ZIP)
sNameXlsxFile is string = fDataDir() + "\Level 1.xlsx"
// Logical name that will be used with the Zip functions
sZipXlsxFile is string = "ZipXlsxFile"
 
// Opening
bZipOpened is boolean
bZipOpened = (zipOpen(sZipXlsxFile, sNameXlsxFile, zipWrite)=0)
// Opening OK?
IF NOT bZipOpened THEN
Error("Failure opening the Excel worksheet", ErrorInfo())
RETURN
END
// Find a file in the ZIP
nSubscriptFilesheet1xmlrels is int
// Exact-match search while specifying the path
nSubscriptFilesheet1xmlrels = zipFindFile(sZipXlsxFile, ...
"xl\worksheets\_rels\sheet1.xml.rels", True)
// File found in the zip?
IF nSubscriptFilesheet1xmlrels < 1 THEN
// File not found
Error("File not found in the XLSX file")
ELSE
// Retrieve the content of the file found
bufFilesheet1xmlrels is Buffer
bufFilesheet1xmlrels = zipExtractFile(sZipXlsxFile, ...
nSubscriptFilesheet1xmlrels, zipInMemory)
// This file is an XML, load it in an XMLDocument variable
xmlSheet1 is xmlDocument = XMLOpen(bufFilesheet1xmlrels, fromString)
IF ErrorOccurred = True THEN
Error("Unable to load the XML file found in the XLSX file", ...
ErrorInfo())
ELSE
// Modify the links to replace the paths
FOR nLinkNum = 1 _TO_ xmlSheet1.Relationships.Relationship..Occurrence
xmlSheet1.Relationships.Relationship[nLinkNum]:Target = ...
Replace(xmlSheet1.Relationships.Relationship[nLinkNum]:Target, ...
"file:///\\formerserver\", "file:///\\newserver\", IgnoreCase)
END
// Retrieve the XML modified in the buffer
bufFilesheet1xmlrels = XMLBuildString(xmlSheet1)
// Delete the file found in the XLSX (in the ZIP)
IF zipDeleteFile(sZipXlsxFile, nSubscriptFilesheet1xmlrels) <> 0 THEN
Error("Unable to delete the XML file found in the XLSX file" + ...
"to update it", ErrorInfo())
ELSE
// Include the new file instead
IF zipAddFile(sZipXlsxFile, bufFilesheet1xmlrels, ...
zipInMemory, "xl\worksheets\_rels\sheet1.xml.rels") <> 0 THEN
Error("Unable to add the new XML file located " + ...
"in the XLSX file to update it", ErrorInfo())
ELSE
Info("Modifications successfully performed")
END
END
END
END
 
// In any case at the end
END:
// If the zip of xlsx is opened, close it
IF bZipOpened THEN zipClose(sZipXlsxFile)
Minimum version required
  • Version 9
This page is also available for…
Comments
Click [Add] to post a comment

Last update: 05/26/2022

Send a report | Local help