use of java.util.zip.ZipEntry in project che by eclipse.
the class ZipFileStructureProvider method initialize.
/**
* Initializes this object's children table based on the contents of
* the specified source file.
*/
protected void initialize() {
children = new HashMap(1000);
Enumeration entries = zipFile.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = (ZipEntry) entries.nextElement();
if (!entry.isDirectory()) {
IPath path = new Path(entry.getName()).addTrailingSeparator();
int pathSegmentCount = path.segmentCount();
for (int i = 1; i < pathSegmentCount; i++) {
createContainer(path.uptoSegment(i));
}
createFile(entry);
}
}
}
use of java.util.zip.ZipEntry in project che by eclipse.
the class InstallExtension method getExtensionFromFile.
private static Extension getExtensionFromFile(Path zipPath) throws IOException {
try (ZipFile zipFile = new ZipFile(zipPath.toString())) {
Enumeration<? extends ZipEntry> entries = zipFile.entries();
ZipEntry gwtXmlEntry = null;
ZipEntry pomEntry = null;
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
if (!entry.isDirectory()) {
if (entry.getName().endsWith(GwtXmlUtils.GWT_MODULE_XML_SUFFIX)) {
gwtXmlEntry = entry;
} else if (entry.getName().endsWith("pom.xml")) {
pomEntry = entry;
}
}
// have both entries
if (pomEntry != null && gwtXmlEntry != null) {
break;
}
}
// TODO consider Codenvy extension validator
if (pomEntry == null) {
return null;
}
String gwtModuleName = null;
if (gwtXmlEntry != null) {
gwtModuleName = gwtXmlEntry.getName().replace(File.separatorChar, '.');
gwtModuleName = gwtModuleName.substring(0, gwtModuleName.length() - GwtXmlUtils.GWT_MODULE_XML_SUFFIX.length());
}
Model pom = Model.readFrom(zipFile.getInputStream(pomEntry));
return new Extension(gwtModuleName, MavenUtils.getGroupId(pom), pom.getArtifactId(), MavenUtils.getVersion(pom));
}
}
use of java.util.zip.ZipEntry in project che by eclipse.
the class ZipArchiverTest method assertThatZipArchiveContainsAllEntries.
private void assertThatZipArchiveContainsAllEntries(InputStream in, Map<String, String> entries) throws Exception {
try (ZipInputStream zip = new ZipInputStream(in)) {
ZipEntry zipEntry;
while ((zipEntry = zip.getNextEntry()) != null) {
String name = zipEntry.getName();
assertTrue(String.format("Unexpected entry %s in zip", name), entries.containsKey(name));
if (!zipEntry.isDirectory()) {
String content = new String(ByteStreams.toByteArray(zip));
assertEquals(String.format("Invalid content of file %s", name), entries.get(name), content);
}
entries.remove(name);
zip.closeEntry();
}
}
assertTrue(String.format("Expected but were not found in zip %s", entries), entries.isEmpty());
}
use of java.util.zip.ZipEntry in project che by eclipse.
the class ZipArchiver method extract.
@Override
public void extract(InputStream zipInput, boolean overwrite, int stripNumber) throws IOException, ForbiddenException, ConflictException, ServerException {
try (ZipInputStream zip = new ZipInputStream(ZipContent.of(zipInput).getContent())) {
InputStream notClosableInputStream = new NotClosableInputStream(zip);
ZipEntry zipEntry;
while ((zipEntry = zip.getNextEntry()) != null) {
VirtualFile extractFolder = folder;
Path relativePath = Path.of(zipEntry.getName());
if (stripNumber > 0) {
if (relativePath.length() <= stripNumber) {
continue;
}
relativePath = relativePath.subPath(stripNumber);
}
if (zipEntry.isDirectory()) {
if (!extractFolder.hasChild(relativePath)) {
extractFolder.createFolder(relativePath.toString());
}
continue;
}
if (relativePath.length() > 1) {
Path neededParentPath = relativePath.getParent();
VirtualFile neededParent = extractFolder.getChild(neededParentPath);
if (neededParent == null) {
neededParent = extractFolder.createFolder(neededParentPath.toString());
}
extractFolder = neededParent;
}
String fileName = relativePath.getName();
VirtualFile file = extractFolder.getChild(Path.of(fileName));
if (file == null) {
extractFolder.createFile(fileName, notClosableInputStream);
} else {
if (overwrite) {
file.updateContent(notClosableInputStream);
} else {
throw new ConflictException(String.format("File '%s' already exists", file.getPath()));
}
}
zip.closeEntry();
}
}
}
use of java.util.zip.ZipEntry in project che by eclipse.
the class ProjectManagerWriteTest method testImportProject.
@Test
public void testImportProject() throws Exception {
ByteArrayOutputStream bout = new ByteArrayOutputStream();
String fileContent = "to be or not to be";
ZipOutputStream zipOut = new ZipOutputStream(bout);
zipOut.putNextEntry(new ZipEntry("folder1/"));
zipOut.putNextEntry(new ZipEntry("folder1/file1.txt"));
zipOut.putNextEntry(new ZipEntry("file1"));
zipOut.write(fileContent.getBytes());
zipOut.close();
final InputStream zip = new ByteArrayInputStream(bout.toByteArray());
final String importType = "_123_";
registerImporter(importType, zip);
SourceStorage sourceConfig = DtoFactory.newDto(SourceStorageDto.class).withType(importType);
pm.importProject("/testImportProject", sourceConfig, false, () -> new ProjectImportOutputWSLineConsumer("BATCH", "ws", 300));
RegisteredProject project = projectRegistry.getProject("/testImportProject");
assertNotNull(project);
// BASE
//System.out.println(">>> "+project.getProjectType());
assertNotNull(project.getBaseFolder().getChild("file1"));
assertEquals(fileContent, project.getBaseFolder().getChild("file1").getVirtualFile().getContentAsString());
}
Aggregations