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

add zip and unzip function #29

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
85 changes: 85 additions & 0 deletions src/main/java/nf_core/nf/test/utils/ZipUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package nf_core.nf.test.utils;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

public class ZipUtils {

public static void unzip(String zipFilePath, String destDirectory) throws IOException {
File destDir = new File(destDirectory);
if (!destDir.exists()) {
destDir.mkdirs();
}

try (ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath))) {
ZipEntry entry = zipIn.getNextEntry();
while (entry != null) {
String filePath = destDirectory + File.separator + entry.getName();
if (!entry.isDirectory()) {
extractFile(zipIn, filePath);
} else {
File dir = new File(filePath);
dir.mkdirs();
}
zipIn.closeEntry();
entry = zipIn.getNextEntry();
}
}
}

private static void extractFile(ZipInputStream zipIn, String filePath) throws IOException {
File file = new File(filePath);

// Create parent directories if they don't exist
File parent = file.getParentFile();
if (parent != null && !parent.exists()) {
parent.mkdirs();
}

// If the entry is a directory, just create it and return
if (filePath.endsWith(File.separator)) {
file.mkdirs();
return;
}

// Extract the file
try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file))) {
byte[] bytesIn = new byte[4096];
int read;
while ((read = zipIn.read(bytesIn)) != -1) {
bos.write(bytesIn, 0, read);
}
}
}

public static void zip(String sourceDirPath, String zipFilePath) throws IOException {
Path zipFile = Files.createFile(Paths.get(zipFilePath));
try (ZipOutputStream zipOut = new ZipOutputStream(Files.newOutputStream(zipFile))) {
Path sourceDir = Paths.get(sourceDirPath);
Files.walkFileTree(sourceDir, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attributes) throws IOException {
Path targetFile = sourceDir.relativize(file);
zipOut.putNextEntry(new ZipEntry(targetFile.toString()));
byte[] bytes = Files.readAllBytes(file);
zipOut.write(bytes, 0, bytes.length);
zipOut.closeEntry();
return FileVisitResult.CONTINUE;
}
});
}
}

}
11 changes: 11 additions & 0 deletions src/main/java/nf_core/nf/test/utils/ZipUtilsWrapper.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package nf_core.nf.test.utils

class ZipUtilsWrapper {
static void unzip(String zipFilePath, String destDirectory) {
ZipUtils.unzip(zipFilePath, destDirectory)
}

static void zip(String sourceDirPath, String zipFilePath) {
ZipUtils.zip(sourceDirPath, zipFilePath)
}
}
10 changes: 10 additions & 0 deletions tests/unzip/main.nf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
workflow {

ch_stable_content = Channel
.of(
"""
I HAVE STABLE CONTENT
""".stripIndent().trim()
)
.collectFile(storeDir: "${params.outdir}/stable", name: 'stable_content.txt', sort: true, newLine: true)
}
26 changes: 26 additions & 0 deletions tests/unzip/main.nf.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import nf_core.nf.test.utils.ZipUtilsWrapper

nextflow_pipeline {

name "Test unzip"
script "./main.nf"
tag "unzip"

test("unzip") {
when {
params {
outdir = "$outputDir"
}
}

then {
ZipUtilsWrapper.zip("${params.outdir}/stable/stable_content.txt", "${params.outdir}/stable_content.zip")
ZipUtilsWrapper.unzip("${params.outdir}/stable_content.zip", "${params.outdir}/stable_content_unzipped/")
def stable_name = getAllFilesFromDir(params.outdir, true, [''], null, ['**'])
assert snapshot(
stable_name
).match()
}
}
}

21 changes: 21 additions & 0 deletions tests/unzip/main.nf.test.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"unzip": {
"content": [
[
[
"stable_content.txt:md5,f6d73f703cda1c725ea78369a6c3a48d"
],
"stable_content.txt:md5,f6d73f703cda1c725ea78369a6c3a48d",
"stable_content.zip:md5,ba7d71d2c8e3cd2a47fa1b498da44e99",
[

]
]
],
"meta": {
"nf-test": "0.9.2",
"nextflow": "24.10.2"
},
"timestamp": "2024-12-16T17:57:07.994203269"
}
}
Loading