Friday, December 1, 2023

Extract files from zip file and upload using X++

 /// <summary>
    /// Extract files from zip file and upload.
    /// </summary>
    /// <param name = "_uploadFileURL">DownLoad of the zip file.</param>
    /// <param name = "_fileSuffixes>List of file suffixes which serves as filter.</param>
    /// <returns>List of file IDs of the unzipped files which meet filter in file suffix list; returns all files if the filter is empty.</returns>
    public static List getUnzippedFiles(SharedServiceUnitURL _uploadFileURL, container _fileSuffixes)
    {
        #File

        System.IO.Compression.ZipArchive zipArchive =
            new System.IO.Compression.ZipArchive(File::UseFileFromURL(_uploadFileURL), System.IO.Compression.ZipArchiveMode::Read);

        CLRObject archiveEntries =  zipArchive.get_Entries();
        int length = archiveEntries.get_Count();

        List uploadUrlList = new List(types::Container);
        container consUnzippedFile;
        int i;

        for (i = 0; i < length; i++)
        {
            System.IO.Compression.ZipArchiveEntry item = archiveEntries.get_Item(i);
            List fileNameComponents = strSplit(item.Name, #FileExtensionDelimiter);
            ListIterator iterator = new ListIterator(fileNameComponents);
            str fileSuffix;
            while (iterator.more())
            {
                fileSuffix = iterator.value();
                iterator.next();
            }

            if (conLen(_fileSuffixes) == 0 || conFind(_fileSuffixes, fileSuffix) > 0)
            {
                using(System.IO.MemoryStream stream = new System.IO.MemoryStream())
                {
                    item.Open().CopyTo(stream);
                    str fileID = BankStatementImportUtils::sendFileToTempStore(stream, item.Name).getFileId();
                    consUnzippedFile = [fileID, item.Name];
                    uploadUrlList.addEnd(consUnzippedFile);
                }
            }
        }

        return uploadUrlList;
    }

No comments:

Post a Comment

Table browser URL in D365FO

Critical Thinking icon icon by Icons8