Запилил вот такое расширение. Файл банковской выписки разбивается на отдельные файлы, по количеству секций начинающихся на
: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', 0, strLen(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(fileInputStream, System.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(fileInputContent, sectionHeader, sectionStartIdx, fileInputContentLength);
while (sectionEndIdx < fileInputContentLength)
{
sectionEndIdx = strScan(fileInputContent, sectionHeader, sectionStartIdx + strLen(sectionHeader), fileInputContentLength);
if (!sectionEndIdx)
{
sectionEndIdx = fileInputContentLength;
}
str sectionContent = subStr(fileInputContent, sectionStartIdx, sectionEndIdx - 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(fileOutputStream, strFmt('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(zipStream, System.IO.Compression.ZipArchiveMode::Create, true))
{
ListEnumerator fileListEnum = _fileList.getEnumerator();
while (fileListEnum.moveNext())
{
str fileId;
str fileName;
[fileId, fileName] = 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;
}
}