Показать сообщение отдельно
Старый 16.11.2022, 13:40   #8  
MorpheusX is offline
MorpheusX
Участник
 
182 / 56 (2) ++++
Регистрация: 04.02.2022
Запилил вот такое расширение. Файл банковской выписки разбивается на отдельные файлы, по количеству секций начинающихся на :20:, и запаковывается в новый ZIP файл. При этом необходимо не забыть указать расширение файлов для импорта в поле BankStatementFormat.FileNameType, в моем случае это txt. Затем подменяем референсы с исходного файла на новый файл в ZIP формате и продолжаем выполнение стандартной логики импорта.

PHP код:
[ExtensionOf(classStr(BankStatementImportBatch))]
final class 
BankStatementImportBatch_Extension
{
    protected List 
importBankStatements()
    {
        
BankStatementFormat bankStatementFormat BankStatementFormat::find(bankStatementFormatRecId);
        
ERModelMappingTable erModelMappingTable ERModelMappingTable::find(bankStatementFormat.ERModelMappingTable);
        
        if (   
bankStatementFormat.UseGERConfiguration
            
&& strFind(erModelMappingTable.Name'MT940'0strLen(erModelMappingTable.Name)))
        {
            List 
splitUploadFileList this.splitUploadFileMT940(uploadFileId);
            
            
FileUploadTemporaryStorageResult fileUploadRet this.addFilesToZipArchive(splitUploadFileList);

            if (   
fileUploadRet != null
                
&& fileUploadRet.getUploadStatus())
            {
                
uploadFileId   fileUploadRet.getFileId();
                
uploadFileName fileUploadRet.getFileName();
                
uploadFileURL  fileUploadRet.getDownloadUrl();
            }
        }
        
        return 
next importBankStatements();
    }

    public List 
splitUploadFileMT940(str _fileId)
    {
        List 
ret = new List(Types::Container);

        
using (System.IO.Stream fileInputStream File::UseFileFromURL(DMFStagingWriter::getDownloadURLFromFileId(_fileId)))
        {
            
System.IO.StreamReader fileStreamReader = new System.IO.StreamReader(fileInputStreamSystem.Text.Encoding::GetEncoding(1252));
            
str                    fileInputContent fileStreamReader.ReadToEnd();
            
            
int fileInputContentLength strLen(fileInputContent);
            
str sectionHeader          '\:20\:';
            
int sectionStartIdx        0;
            
int sectionEndIdx          0;
            
int fileNum                0;

            
sectionStartIdx strFind(fileInputContentsectionHeadersectionStartIdxfileInputContentLength);

            while (
sectionEndIdx fileInputContentLength)
            {
                
sectionEndIdx strScan(fileInputContentsectionHeadersectionStartIdx strLen(sectionHeader), fileInputContentLength);
                
                if (!
sectionEndIdx)
                {
                    
sectionEndIdx fileInputContentLength;
                }

                
str sectionContent subStr(fileInputContentsectionStartIdxsectionEndIdx sectionStartIdx);
                
                
using (System.IO.Stream fileOutputStream = new System.IO.MemoryStream())
                {
                    
using (System.IO.StreamWriter fileOutputStreamWriter = new System.IO.StreamWriter(fileOutputStream))
                    {
                        
fileOutputStreamWriter.Write(sectionContent);
                        
fileOutputStreamWriter.Flush();
                
                        
fileNum++;

                        
FileUploadTemporaryStorageResult fileUploadRet File::SendFileToTempStore_GetResult(fileOutputStreamstrFmt('File_%1.txt'fileNum));

                        if (   
fileUploadRet != null
                            
&& fileUploadRet.getUploadStatus())
                        {
                            
ret.addEnd([fileUploadRet.getFileId(), fileUploadRet.getFileName()]);
                        }
                    }
                }
                
                
sectionStartIdx sectionEndIdx;
            }
        }

        return 
ret;
    }

    public 
FileUploadTemporaryStorageResult addFilesToZipArchive(List _fileList)
    {
        
FileUploadTemporaryStorageResult ret;

        
using (System.IO.MemoryStream zipStream = new System.IO.MemoryStream())
        {
            
using (System.IO.Compression.ZipArchive zipArchive = new System.IO.Compression.ZipArchive(zipStreamSystem.IO.Compression.ZipArchiveMode::Createtrue))
            {
                
ListEnumerator fileListEnum _fileList.getEnumerator();

                while (
fileListEnum.moveNext())
                {
                    
str fileId;
                    
str fileName;

                    [
fileIdfileName] = fileListEnum.current();

                    
using (System.IO.MemoryStream file File::UseFileFromURL(DMFStagingWriter::getDownloadURLFromFileId(fileId)))
                    {
                        
System.IO.Compression.ZipArchiveEntry fileEntry zipArchive.CreateEntry(fileName);
                        
                        
using (System.IO.Stream fileEntryStream fileEntry.Open())
                        {
                            
file.CopyTo(fileEntryStream);
                        }
                    }
                }
            }

            
ret File::SendFileToTempStore_GetResult(zipStream'ArchiveFile.zip');
        }

        return 
ret;
    }



Последний раз редактировалось MorpheusX; 16.11.2022 в 14:46.
За это сообщение автора поблагодарили: EVGL (10).