use of java.util.zip.ZipException in project intellij-community by JetBrains.
the class Digester method digestZipFile.
public static long digestZipFile(File file) throws IOException {
ZipFile zipFile;
try {
zipFile = new ZipFile(file);
} catch (ZipException e) {
// This was not a zip file...
return digestRegularFile(file, false);
}
try {
List<ZipEntry> sorted = new ArrayList<>();
Enumeration<? extends ZipEntry> temp = zipFile.entries();
while (temp.hasMoreElements()) {
ZipEntry each = temp.nextElement();
if (!each.isDirectory()) {
sorted.add(each);
}
}
Collections.sort(sorted, Comparator.comparing(ZipEntry::getName));
CRC32 crc = new CRC32();
for (ZipEntry each : sorted) {
try (InputStream in = zipFile.getInputStream(each)) {
doDigestStream(in, crc);
}
}
return crc.getValue();
} finally {
zipFile.close();
}
}
use of java.util.zip.ZipException in project gerrit by GerritCodeReview.
the class InstallPlugin method apply.
@Override
public Response<PluginInfo> apply(TopLevelResource resource, Input input) throws BadRequestException, MethodNotAllowedException, IOException {
if (!loader.isRemoteAdminEnabled()) {
throw new MethodNotAllowedException("remote installation is disabled");
}
try {
try (InputStream in = openStream(input)) {
String pluginName = loader.installPluginFromStream(name, in);
ListPlugins.PluginInfo info = new ListPlugins.PluginInfo(loader.get(pluginName));
return created ? Response.created(info) : Response.ok(info);
}
} catch (PluginInstallException e) {
StringWriter buf = new StringWriter();
buf.write(String.format("cannot install %s", name));
if (e.getCause() instanceof ZipException) {
buf.write(": ");
buf.write(e.getCause().getMessage());
} else {
buf.write(":\n");
PrintWriter pw = new PrintWriter(buf);
e.printStackTrace(pw);
pw.flush();
}
throw new BadRequestException(buf.toString());
}
}
use of java.util.zip.ZipException in project intellij-community by JetBrains.
the class GradleImportingTestCase method configureWrapper.
private void configureWrapper() throws IOException, URISyntaxException {
final URI distributionUri = new DistributionLocator().getDistributionFor(GradleVersion.version(gradleVersion));
myProjectSettings.setDistributionType(DistributionType.DEFAULT_WRAPPED);
final VirtualFile wrapperJarFrom = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(wrapperJar());
assert wrapperJarFrom != null;
final VirtualFile wrapperJarFromTo = createProjectSubFile("gradle/wrapper/gradle-wrapper.jar");
new WriteAction() {
@Override
protected void run(@NotNull Result result) throws Throwable {
wrapperJarFromTo.setBinaryContent(wrapperJarFrom.contentsToByteArray());
}
}.execute().throwException();
Properties properties = new Properties();
properties.setProperty("distributionBase", "GRADLE_USER_HOME");
properties.setProperty("distributionPath", "wrapper/dists");
properties.setProperty("zipStoreBase", "GRADLE_USER_HOME");
properties.setProperty("zipStorePath", "wrapper/dists");
properties.setProperty("distributionUrl", distributionUri.toString());
StringWriter writer = new StringWriter();
properties.store(writer, null);
createProjectSubFile("gradle/wrapper/gradle-wrapper.properties", writer.toString());
WrapperConfiguration wrapperConfiguration = GradleUtil.getWrapperConfiguration(getProjectPath());
PathAssembler.LocalDistribution localDistribution = new PathAssembler(StartParameter.DEFAULT_GRADLE_USER_HOME).getDistribution(wrapperConfiguration);
File zip = localDistribution.getZipFile();
try {
if (zip.exists()) {
ZipFile zipFile = new ZipFile(zip);
zipFile.close();
}
} catch (ZipException e) {
e.printStackTrace();
System.out.println("Corrupted file will be removed: " + zip.getPath());
FileUtil.delete(zip);
} catch (IOException e) {
e.printStackTrace();
}
}
use of java.util.zip.ZipException in project phoenix by apache.
the class ResourceList method getResourcesFromJarFile.
// Visible for testing
Collection<String> getResourcesFromJarFile(final File file, final Pattern pattern) {
final List<String> retVal = new ArrayList<>();
ZipFile zf;
try {
zf = new ZipFile(file);
} catch (FileNotFoundException e) {
// Gracefully handle a jar listed on the classpath that doesn't actually exist.
return Collections.emptyList();
} catch (final ZipException e) {
throw new Error(e);
} catch (final IOException e) {
throw new Error(e);
}
final Enumeration e = zf.entries();
while (e.hasMoreElements()) {
final ZipEntry ze = (ZipEntry) e.nextElement();
final String fileName = ze.getName();
final boolean accept = pattern.matcher(fileName).matches();
logger.trace("fileName:" + fileName);
logger.trace("File:" + file.toString());
logger.trace("Match:" + accept);
if (accept) {
logger.trace("Adding File from Jar: " + fileName);
retVal.add("/" + fileName);
}
}
try {
zf.close();
} catch (final IOException e1) {
throw new Error(e1);
}
return retVal;
}
use of java.util.zip.ZipException in project robovm by robovm.
the class ZipFileTest method testSTORED.
public void testSTORED() throws IOException {
ZipOutputStream out = createZipOutputStream(createTemporaryZipFile());
CRC32 crc = new CRC32();
// Missing CRC, size, and compressed size => failure.
try {
ZipEntry ze = new ZipEntry("a");
ze.setMethod(ZipEntry.STORED);
out.putNextEntry(ze);
fail();
} catch (ZipException expected) {
}
// Missing CRC and compressed size => failure.
try {
ZipEntry ze = new ZipEntry("a");
ze.setMethod(ZipEntry.STORED);
ze.setSize(0);
out.putNextEntry(ze);
fail();
} catch (ZipException expected) {
}
// Missing CRC and size => failure.
try {
ZipEntry ze = new ZipEntry("a");
ze.setMethod(ZipEntry.STORED);
ze.setSize(0);
ze.setCompressedSize(0);
out.putNextEntry(ze);
fail();
} catch (ZipException expected) {
}
// Missing size and compressed size => failure.
try {
ZipEntry ze = new ZipEntry("a");
ze.setMethod(ZipEntry.STORED);
ze.setCrc(crc.getValue());
out.putNextEntry(ze);
fail();
} catch (ZipException expected) {
}
// Missing size is copied from compressed size.
{
ZipEntry ze = new ZipEntry("okay1");
ze.setMethod(ZipEntry.STORED);
ze.setCrc(crc.getValue());
assertEquals(-1, ze.getSize());
assertEquals(-1, ze.getCompressedSize());
ze.setCompressedSize(0);
assertEquals(-1, ze.getSize());
assertEquals(0, ze.getCompressedSize());
out.putNextEntry(ze);
assertEquals(0, ze.getSize());
assertEquals(0, ze.getCompressedSize());
}
// Missing compressed size is copied from size.
{
ZipEntry ze = new ZipEntry("okay2");
ze.setMethod(ZipEntry.STORED);
ze.setCrc(crc.getValue());
assertEquals(-1, ze.getSize());
assertEquals(-1, ze.getCompressedSize());
ze.setSize(0);
assertEquals(0, ze.getSize());
assertEquals(-1, ze.getCompressedSize());
out.putNextEntry(ze);
assertEquals(0, ze.getSize());
assertEquals(0, ze.getCompressedSize());
}
// Mismatched size and compressed size => failure.
try {
ZipEntry ze = new ZipEntry("a");
ze.setMethod(ZipEntry.STORED);
ze.setCrc(crc.getValue());
ze.setCompressedSize(1);
ze.setSize(0);
out.putNextEntry(ze);
fail();
} catch (ZipException expected) {
}
// Everything present => success.
ZipEntry ze = new ZipEntry("okay");
ze.setMethod(ZipEntry.STORED);
ze.setCrc(crc.getValue());
ze.setSize(0);
ze.setCompressedSize(0);
out.putNextEntry(ze);
out.close();
}
Aggregations