Skip to content

Commit

Permalink
Implements changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreaLaGrotteria committed Nov 12, 2024
1 parent f982256 commit 1af444b
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 31 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,5 @@ buildSrc/src/generated
*.scpt
.nova
.run
buildSrc/src/test/resources/guides/*.zip

cli/bin
Original file line number Diff line number Diff line change
@@ -1,51 +1,55 @@
package io.micronaut.guides.core;

import jakarta.inject.Singleton;
import org.apache.commons.compress.archivers.zip.UnixStat;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
import org.apache.commons.compress.utils.IOUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

@Singleton
public class DefaultGuideProjectZipper implements GuideProjectZipper {
private static final List<String> EXCLUDED_FILES = List.of(".idea", ".DS_Store");
private static final List<String> EXECUTABLES = List.of("gradlew", "gradlew.bat", "mvnw", "mvnw.bat");

private static void zipDirectoryContents(File folder, String baseName, ZipOutputStream zipOutputStream) throws IOException {
File[] files = folder.listFiles();
if (files == null) return;

for (File file : files) {
private static void compressDirectoryToZipfile(String rootDir, String sourceDir, ZipArchiveOutputStream out) throws IOException {
for (File file : new File(sourceDir).listFiles()) {
if (EXCLUDED_FILES.contains(file.getName())) {
continue;
}
if (file.isDirectory()) {
zipDirectoryContents(file, baseName + "/" + file.getName(), zipOutputStream);
compressDirectoryToZipfile(rootDir, sourceDir + File.separatorChar + file.getName(), out);
} else {
try (FileInputStream fis = new FileInputStream(file)) {
String zipEntryName = baseName + "/" + file.getName();
zipOutputStream.putNextEntry(new ZipEntry(zipEntryName));

byte[] buffer = new byte[1024];
int length;
while ((length = fis.read(buffer)) > 0) {
zipOutputStream.write(buffer, 0, length);
}
zipOutputStream.closeEntry();
String zipPath = sourceDir.replace(rootDir, "") + '/' + file.getName();
if (zipPath.charAt(0) == '/') {
zipPath = zipPath.substring(1);
}
ZipArchiveEntry entry = new ZipArchiveEntry(zipPath);
if (EXECUTABLES.contains(file.getName())) {
entry.setUnixMode(UnixStat.FILE_FLAG | 0755);
}
out.putArchiveEntry(entry);
Path p = Paths.get(sourceDir, file.getName());
FileInputStream in = new FileInputStream(p.toFile());
IOUtils.copy(in, out);
IOUtils.closeQuietly(in);
out.closeArchiveEntry();
}
}
}

@Override
public void zipDirectory(File folderToZip, File zipFile) throws IOException {
try (FileOutputStream fileOutputStream = new FileOutputStream(zipFile);
ZipOutputStream zipOutputStream = new ZipOutputStream(fileOutputStream)) {
zipDirectoryContents(folderToZip, folderToZip.getName(), zipOutputStream);
}
public void zipDirectory(String sourceDir, String outputFile) throws IOException {
ZipArchiveOutputStream zipOutputStream = new ZipArchiveOutputStream(new FileOutputStream(outputFile));
compressDirectoryToZipfile(sourceDir, sourceDir, zipOutputStream);
IOUtils.closeQuietly(zipOutputStream);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
import io.micronaut.core.annotation.NonNull;
import jakarta.validation.constraints.NotNull;

import java.io.File;
import java.io.IOException;

public interface GuideProjectZipper {

void zipDirectory(@NonNull @NotNull File folderToZip, @NonNull @NotNull File zipFile) throws IOException;
void zipDirectory(@NonNull @NotNull String sourceDir, @NonNull @NotNull String outputFile) throws IOException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import jakarta.inject.Inject;
import org.junit.jupiter.api.Test;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.LinkedList;
Expand All @@ -23,12 +22,11 @@ public class GuideProjectZipperTest {
@Test
void testZip() throws IOException {
String projectFolder = "src/test/resources/guides/creating-your-first-micronaut-app";
File projectFile = new File(projectFolder);
File zipFile = new File(projectFolder + ".zip");
String zipFile = "build/tmp/test/creating-your-first-micronaut-app.zip";

guideProjectZipper.zipDirectory(projectFile, zipFile);
guideProjectZipper.zipDirectory(projectFolder, zipFile);

List<String> expected = List.of("creating-your-first-micronaut-app/metadata.json", "creating-your-first-micronaut-app/creating-your-first-micronaut-app.adoc");
List<String> expected = List.of("metadata.json", "creating-your-first-micronaut-app.adoc");
List<String> result = new LinkedList<>();

try (ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile))) {
Expand Down

0 comments on commit 1af444b

Please sign in to comment.