Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return archive in zip stream #41

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion afs-core/src/main/java/com/powsybl/afs/AbstractNodeBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

import com.powsybl.afs.storage.AppStorage;
import com.powsybl.afs.storage.AppStorageArchive;
import com.powsybl.afs.storage.Utils;
import com.powsybl.afs.storage.NodeInfo;
import com.powsybl.afs.storage.Utils;

import java.io.IOException;
import java.io.UncheckedIOException;
Expand All @@ -22,6 +22,7 @@
import java.time.ZonedDateTime;
import java.util.*;
import java.util.stream.Stream;
import java.util.zip.ZipInputStream;

/**
* Base class for all node objects stored in an AFS tree.
Expand Down Expand Up @@ -192,6 +193,23 @@ public void archive(Path dir, boolean useZip, boolean archiveDependencies, Map<S
}
}

/**
*
* @param dir The directory to archive
* @param archiveDependencies
* @param outputBlackList
* @return Returns an ZipInputStream representing the archived file
*/
public ZipInputStream openArchiveStream(Path dir, boolean archiveDependencies, Map<String, List<String>> outputBlackList) {
Objects.requireNonNull(dir);
try {
new AppStorageArchive(storage).archive(info.getId(), dir, archiveDependencies, outputBlackList);
return Utils.zip(dir, true);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}

public void unarchive(Path dir, boolean isZipped) {
if (isZipped) {
String filename = dir.getFileName().toString().substring(0, dir.getFileName().toString().length() - 4);
Expand Down
23 changes: 18 additions & 5 deletions afs-core/src/test/java/com/powsybl/afs/AfsBaseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@
import com.google.common.jimfs.Configuration;
import com.google.common.jimfs.Jimfs;
import com.powsybl.afs.mapdb.storage.MapDbAppStorage;
import com.powsybl.afs.storage.AfsStorageException;
import com.powsybl.afs.storage.AppStorage;
import com.powsybl.afs.storage.InMemoryEventsBus;
import com.powsybl.afs.storage.NodeGenericMetadata;
import com.powsybl.afs.storage.NodeInfo;
import com.powsybl.afs.storage.*;
import com.powsybl.computation.ComputationManager;
import com.powsybl.iidm.network.NetworkFactoryService;
import org.junit.After;
Expand All @@ -31,6 +27,7 @@
import java.nio.file.Path;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.zip.ZipInputStream;

import static org.junit.Assert.*;

Expand Down Expand Up @@ -250,6 +247,22 @@ public void archiveAndUnarchiveTestWithZip() throws IOException {
assertEquals(1, dir2.getChildren().size());
}

@Test
public void archiveInMemory() throws IOException {
Project project = afs.getRootFolder().createProject("test");
ProjectFolder rootFolder = project.getRootFolder();
ProjectFolder dir1 = rootFolder.createFolder("dir1");
String nodeId = dir1.getId();
Path rootDir = fileSystem.getPath("/root");
Files.createDirectory(rootDir);
Files.createDirectory(rootDir.resolve("test"));
try (ZipInputStream zis = dir1.openArchiveStream(rootDir.resolve("test"), true, new HashMap<>())) {
assertEquals(nodeId + "/", zis.getNextEntry().getName());
assertEquals(nodeId + "/info.json", zis.getNextEntry().getName());
assertNull(zis.getNextEntry());
}
}

@Test
public void archiveAndUnarchiveTestWithDirAndBlackList() throws IOException {
Project project = afs.getRootFolder().createProject("test");
Expand Down
53 changes: 38 additions & 15 deletions afs-storage-api/src/main/java/com/powsybl/afs/storage/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,29 +44,52 @@ private Utils() throws IllegalAccessException {
*/
public static void zip(Path dir, Path zipPath, boolean deleteDirectory) throws IOException {
try (ZipOutputStream zos = new ZipOutputStream(Files.newOutputStream(zipPath));
Stream<Path> walk = Files.walk(dir);) {
walk.filter(someFileToZip -> !someFileToZip.equals(dir))
.forEach(
someFileToZip -> {
Path pathInZip = dir.relativize(someFileToZip);
try {
if (Files.isDirectory(someFileToZip)) {
addDirectory(zos, pathInZip);
} else {
addFile(zos, someFileToZip, pathInZip);
}
} catch (IOException e) {
throw new AfsStorageException(e.getMessage());
}
Stream<Path> walk = Files.walk(dir)) {
zip(walk, dir, zos);
} catch (IOException | AfsStorageException e) {
throw new IOException(e);
}

});
if (deleteDirectory) {
deleteDirectory(dir);
}
}

public static ZipInputStream zip(Path dir, boolean deleteDirectory) throws IOException {
byte[] bytes;
try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
ZipOutputStream zos = new ZipOutputStream(baos);
Stream<Path> walk = Files.walk(dir)) {
zip(walk, dir, zos);
zos.close();
baos.close();
bytes = baos.toByteArray();
} catch (IOException | AfsStorageException e) {
throw new IOException(e);
}

if (deleteDirectory) {
deleteDirectory(dir);
}
return new ZipInputStream(new ByteArrayInputStream(bytes));
}
Comment on lines +58 to +75
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you creating a pipe (i.e. creating a zip from a path, and returning a input-stream for this path)?
In that case, could you use PipedInputStream/PipedOutputStream as it's done in NetworkXml.copy?


private static void zip(Stream<Path> walk, Path dir, ZipOutputStream zos) {
walk.filter(someFileToZip -> !someFileToZip.equals(dir))
.forEach(
someFileToZip -> {
Path pathInZip = dir.relativize(someFileToZip);
try {
if (Files.isDirectory(someFileToZip)) {
addDirectory(zos, pathInZip);
} else {
addFile(zos, someFileToZip, pathInZip);
}
} catch (IOException e) {
throw new AfsStorageException(e.getMessage());
}
Comment on lines +82 to +90
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To make this lambda smaller, you should catch exception in addDirectory and addFile


});
}

/**
Expand Down