Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
Signed-off-by: Dmytro Kulieshov <dkuliesh@redhat.com>
  • Loading branch information
Dmytro Kulieshov committed Apr 13, 2018
1 parent fe6bc26 commit 24ec282
Showing 1 changed file with 20 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,35 @@
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
import javax.inject.Inject;
import javax.inject.Singleton;
import org.apache.commons.io.IOUtils;
import org.eclipse.che.api.core.ServerException;
import org.eclipse.che.api.fs.server.PathTransformer;
import org.eclipse.che.api.fs.server.WsPathUtils;

@Singleton
class ZipArchiver {

private static void zip(File zipInFile, String fileName, ZipOutputStream zos) throws IOException {
private final Path root;

@Inject
ZipArchiver(PathTransformer pathTransformer) {
this.root = pathTransformer.transform(WsPathUtils.ROOT);
}

private static void zip(Path zipRoot, File zipInFile, ZipOutputStream zos) throws IOException {
if (zipInFile.isDirectory()) {
File[] files = zipInFile.listFiles();
for (File file : files == null ? new File[0] : files) {
zip(file, file.getAbsolutePath(), zos);
zip(zipRoot, file, zos);
}
return;
}

try (FileInputStream fis = new FileInputStream(zipInFile); ) {
ZipEntry zipEntry = new ZipEntry(fileName);
String zipEntryName = zipRoot.relativize(zipInFile.toPath()).toString();
ZipEntry zipEntry = new ZipEntry(zipEntryName);
zos.putNextEntry(zipEntry);
IOUtils.copy(fis, zos);
}
Expand All @@ -53,7 +64,12 @@ InputStream zip(Path fsPath) throws ServerException {

try (FileOutputStream fos = new FileOutputStream(outFile);
ZipOutputStream zos = new ZipOutputStream(fos)) {
zip(inFile, inFile.getName(), zos);
Path parent = fsPath.getParent();
if (root.startsWith(parent)) {
zip(root, inFile, zos);
} else {
zip(parent, inFile, zos);
}
}

return newInputStream(outFile.toPath());
Expand Down

0 comments on commit 24ec282

Please sign in to comment.