Skip to content

Commit

Permalink
Unification / cleanup / nio usage for VirtualArtifactRepository
Browse files Browse the repository at this point in the history
  • Loading branch information
laeubi committed Dec 15, 2023
1 parent d4ea9e0 commit e208ee3
Showing 1 changed file with 15 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.URI;
Expand All @@ -26,7 +26,9 @@
import java.util.Set;
import java.util.UUID;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.JarOutputStream;
import java.util.jar.Manifest;
import java.util.zip.ZipOutputStream;

import org.eclipse.core.runtime.Assert;
Expand Down Expand Up @@ -127,14 +129,11 @@ private void transferArtifact(Object artifactModel, OutputStream destination) th
if (location == null) {
throw new FileNotFoundException(bundleInfo.getSymbolicName());
}

File bundleLocation = new File(location);
if (bundleLocation.isFile()) {
try (InputStream is = new FileInputStream(bundleLocation)) {
is.transferTo(destination);
}
Files.copy(bundleLocation.toPath(), destination);
} else {
ZipOutputStream zip = new ZipOutputStream(destination);
ZipOutputStream zip = getZipStream(destination, bundleLocation);
FileUtils.zip(zip, bundleLocation, Set.of(), FileUtils.createRootPathComputer(bundleLocation));
zip.finish();
zip.flush();
Expand All @@ -161,6 +160,16 @@ private void transferArtifact(Object artifactModel, OutputStream destination) th
}
}

private ZipOutputStream getZipStream(OutputStream destination, File bundleLocation) throws IOException {
File manifestFile = new File(bundleLocation, JarFile.MANIFEST_NAME);
if (manifestFile.isFile()) {
try (FileInputStream fileInputStream = new FileInputStream(manifestFile)) {
return new JarOutputStream(destination, new Manifest(fileInputStream));
}
}
return new ZipOutputStream(destination);
}

@Override
public IQueryable<IArtifactDescriptor> descriptorQueryable() {
return (query, monitor) -> query.perform(artifacts.keySet().iterator());
Expand Down

0 comments on commit e208ee3

Please sign in to comment.