-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f982256
commit 1af444b
Showing
4 changed files
with
31 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,5 @@ buildSrc/src/generated | |
*.scpt | ||
.nova | ||
.run | ||
buildSrc/src/test/resources/guides/*.zip | ||
|
||
cli/bin |
50 changes: 27 additions & 23 deletions
50
buildSrc/src/main/java/io/micronaut/guides/core/DefaultGuideProjectZipper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters