From 0b1a1dfd8e5d0b9b04a819cb05fcfc9102537f84 Mon Sep 17 00:00:00 2001 From: Geoffroy Jamgotchian Date: Sun, 12 Nov 2023 23:31:06 +0100 Subject: [PATCH] Fix Signed-off-by: Geoffroy Jamgotchian --- .../com/powsybl/python/network/NetworkCFunctions.java | 9 ++++----- pypowsybl/network/impl/network.py | 2 ++ tests/test_network.py | 3 ++- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/java/src/main/java/com/powsybl/python/network/NetworkCFunctions.java b/java/src/main/java/com/powsybl/python/network/NetworkCFunctions.java index bdcf965f83..052bd53c9a 100644 --- a/java/src/main/java/com/powsybl/python/network/NetworkCFunctions.java +++ b/java/src/main/java/com/powsybl/python/network/NetworkCFunctions.java @@ -277,16 +277,15 @@ public static ArrayPointer saveNetworkToBinaryBuffer(IsolateThread Reporter reporter = ReportCUtils.getReporter(reporterHandle); // to support all kind of export: simple file or multiple to an archive, // best is to write to a zip file - try (ByteArrayOutputStream bos = new ByteArrayOutputStream(); - ZipOutputStream zos = new ZipOutputStream(bos)) { + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + try (ZipOutputStream zos = new ZipOutputStream(bos)) { DataSource dataSource = new ZipMemDataSource("file", zos); exporter.export(network, parameters, dataSource, reporter); - bos.flush(); - byte[] bytes = bos.toByteArray(); - return Util.createByteArray(bytes); } catch (IOException e) { throw new UncheckedIOException(e); } + byte[] bytes = bos.toByteArray(); + return Util.createByteArray(bytes); }); } diff --git a/pypowsybl/network/impl/network.py b/pypowsybl/network/impl/network.py index a288655cd0..c254b86e3f 100644 --- a/pypowsybl/network/impl/network.py +++ b/pypowsybl/network/impl/network.py @@ -183,6 +183,8 @@ def save_to_string(self, format: str = 'XIIDM', parameters: ParamsDict = None, r def save_to_binary_buffer(self, format: str = 'XIIDM', parameters: ParamsDict = None, reporter: Reporter = None) -> io.BytesIO: """ Save a network to a binary buffer using a specified format. + In the current implementation, whatever the specified format is (so a format creating a single file or a format + creating multiple files), the created binary buffer is a zip file. Args: format: format to export, only support mono file type, defaults to 'XIIDM' diff --git a/tests/test_network.py b/tests/test_network.py index 40b580f43d..bf2bcb1bc0 100644 --- a/tests/test_network.py +++ b/tests/test_network.py @@ -65,7 +65,8 @@ def test_save_cgmes_zip(): n = pp.network.create_eurostag_tutorial_example1_network() buffer = n.save_to_binary_buffer(format='CGMES') with zipfile.ZipFile(buffer, 'r') as zip_file: - assert [''] == zip_file.namelist() + assert ['file_EQ.xml', 'file_TP.xml', 'file_SSH.xml', 'file_SV.xml'] == zip_file.namelist() + def test_load_zipped_xiidm(): with open(DATA_DIR.joinpath('battery_xiidm.zip'), "rb") as fh: