use of java.util.zip.ZipException in project mdw-designer by CenturyLinkCloud.
the class ProjectUpdater method addWebLibs.
/**
* Update WEB-INF/lib jars in MDWWeb.war with contents of zip from the MDW
* Release site. (Assumes a clean MDWWeb.war without any extensions.)
*/
public void addWebLibs(String zipFile, String webAppWarName, List<DescriptorUpdater> descriptorUpdaters, boolean doDownload, boolean deleteZip, IProgressMonitor monitor) throws InvocationTargetException, InterruptedException, CoreException, IOException {
IFile webAppWar;
if (workflowProject.isCloudProject()) {
project = workflowProject.getSourceProject();
localFolder = workflowProject.getSourceProject().getFolder(MdwPlugin.getSettings().getTempResourceLocation());
IFolder deployEarFolder = workflowProject.getSourceProject().getFolder(new Path("deploy/ear"));
webAppWar = deployEarFolder.getFile(webAppWarName);
} else {
project = workflowProject.getEarProject();
localFolder = workflowProject.getEarContentFolder();
webAppWar = localFolder.getFile(webAppWarName);
}
if (!webAppWar.exists())
// may not exist in some scenarios
return;
file = project.getFile(localFolder.getProjectRelativePath().toString() + "/" + zipFile);
IFile newWebAppWar = localFolder.getFile(webAppWarName + ".tmp");
ZipFile zip = null;
JarFile warArchive = null;
JarOutputStream jarOut = null;
BufferedOutputStream bufferedOut = null;
monitor.beginTask("Building " + webAppWarName, 200);
try {
monitor.worked(5);
if (doDownload)
PluginUtil.downloadIntoProject(project, getFileUrl(), localFolder, file, "Download", // 75 ticks
monitor);
else
monitor.worked(75);
File outFile = new File(newWebAppWar.getLocationURI());
bufferedOut = new BufferedOutputStream(new FileOutputStream(outFile));
jarOut = new JarOutputStream(bufferedOut);
int read = 0;
byte[] buf = new byte[1024];
monitor.subTask("Add existing entries to WAR file");
SubProgressMonitor subMonitor = new SubProgressMonitor(monitor, 50);
// lots of
subMonitor.beginTask("Add existing entries", 500);
// entries
subMonitor.worked(5);
warArchive = new JarFile(new File(webAppWar.getLocationURI()));
for (Enumeration<?> entriesEnum = warArchive.entries(); entriesEnum.hasMoreElements(); ) {
JarEntry jarEntry = (JarEntry) entriesEnum.nextElement();
DescriptorUpdater dUpdater = null;
if (descriptorUpdaters != null) {
for (DescriptorUpdater descriptorUpdater : descriptorUpdaters) {
if (descriptorUpdater.getFilePath().equals(jarEntry.getName())) {
dUpdater = descriptorUpdater;
break;
}
}
}
InputStream warIn = warArchive.getInputStream(jarEntry);
if (dUpdater == null) {
// straight pass-through
jarOut.putNextEntry(jarEntry);
while ((read = warIn.read(buf)) != -1) jarOut.write(buf, 0, read);
} else {
jarOut.putNextEntry(new JarEntry(jarEntry.getName()));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// files)
while ((read = warIn.read(buf)) != -1) baos.write(buf, 0, read);
baos.flush();
byte[] contents = dUpdater.processContents(new String(baos.toByteArray()), monitor).getBytes();
jarOut.write(contents, 0, contents.length);
}
warIn.close();
subMonitor.worked(1);
}
subMonitor.done();
monitor.subTask("Add new entries to WAR file");
subMonitor = new SubProgressMonitor(monitor, 50);
// potentially lots of
subMonitor.beginTask("Add new entries", 500);
// entries
subMonitor.worked(5);
zip = new ZipFile(new File(file.getLocationURI()));
for (Enumeration<?> entriesEnum = zip.entries(); entriesEnum.hasMoreElements(); ) {
ZipEntry zipEntry = (ZipEntry) entriesEnum.nextElement();
try {
jarOut.putNextEntry(zipEntry);
} catch (ZipException ex) {
// ignore duplicate entries
if (ex.getMessage().startsWith("duplicate entry:"))
PluginMessages.log(ex.getMessage());
else
throw ex;
}
InputStream zipIn = zip.getInputStream(zipEntry);
while ((read = zipIn.read(buf)) != -1) jarOut.write(buf, 0, read);
zipIn.close();
subMonitor.worked(1);
}
subMonitor.done();
} finally {
if (zip != null)
zip.close();
if (warArchive != null)
warArchive.close();
if (jarOut != null)
jarOut.close();
if (bufferedOut != null)
bufferedOut.close();
SubProgressMonitor subMon = monitor.isCanceled() ? null : new SubProgressMonitor(monitor, 25);
monitor.subTask("Cleaning up");
if (deleteZip) {
file.refreshLocal(1, subMon);
file.delete(true, subMon);
}
webAppWar.refreshLocal(1, subMon);
if (webAppWar.exists())
webAppWar.delete(true, subMon);
newWebAppWar.refreshLocal(1, subMon);
if (newWebAppWar.exists())
newWebAppWar.move(webAppWar.getFullPath(), true, subMon);
}
}
use of java.util.zip.ZipException in project phosphor by gmu-swe.
the class Instrumenter method processZip.
/**
* Handle Jar file, Zip file and War file
*/
public static void processZip(File f, File outputDir) {
try {
ZipFile zip = new ZipFile(f);
ZipOutputStream zos = null;
zos = new ZipOutputStream(new FileOutputStream(outputDir.getPath() + File.separator + f.getName()));
Enumeration<? extends ZipEntry> entries = zip.entries();
while (entries.hasMoreElements()) {
ZipEntry e = entries.nextElement();
if (e.getName().endsWith(".class")) {
if (ANALYZE_ONLY)
analyzeClass(zip.getInputStream(e));
else {
try {
ZipEntry outEntry = new ZipEntry(e.getName());
zos.putNextEntry(outEntry);
byte[] clazz = instrumentClass(f.getAbsolutePath(), zip.getInputStream(e), true);
if (clazz == null) {
System.out.println("Failed to instrument " + e.getName() + " in " + f.getName());
InputStream is = zip.getInputStream(e);
byte[] buffer = new byte[1024];
while (true) {
int count = is.read(buffer);
if (count == -1)
break;
zos.write(buffer, 0, count);
}
is.close();
} else
zos.write(clazz);
zos.closeEntry();
} catch (ZipException ex) {
ex.printStackTrace();
continue;
}
}
} else if (e.getName().endsWith(".jar")) {
ZipEntry outEntry = new ZipEntry(e.getName());
Random r = new Random();
String markFileName = Long.toOctalString(System.currentTimeMillis()) + Integer.toOctalString(r.nextInt(10000)) + e.getName().replace("/", "");
File tmp = new File("/tmp/" + markFileName);
if (tmp.exists())
tmp.delete();
FileOutputStream fos = new FileOutputStream(tmp);
byte[] buf = new byte[1024];
int len;
InputStream is = zip.getInputStream(e);
while ((len = is.read(buf)) > 0) {
fos.write(buf, 0, len);
}
is.close();
fos.close();
File tmp2 = new File("/tmp/tmp2");
if (!tmp2.exists())
tmp2.mkdir();
processZip(tmp, tmp2);
tmp.delete();
zos.putNextEntry(outEntry);
is = new FileInputStream("/tmp/tmp2/" + markFileName);
byte[] buffer = new byte[1024];
while (true) {
int count = is.read(buffer);
if (count == -1)
break;
zos.write(buffer, 0, count);
}
is.close();
zos.closeEntry();
} else {
ZipEntry outEntry = new ZipEntry(e.getName());
if (e.isDirectory()) {
try {
zos.putNextEntry(outEntry);
zos.closeEntry();
} catch (ZipException exxxx) {
System.out.println("Ignoring exception: " + exxxx.getMessage());
}
} else if (e.getName().startsWith("META-INF") && (e.getName().endsWith(".SF") || e.getName().endsWith(".RSA"))) {
// don't copy this
} else if (e.getName().equals("META-INF/MANIFEST.MF")) {
Scanner s = new Scanner(zip.getInputStream(e));
zos.putNextEntry(outEntry);
String curPair = "";
while (s.hasNextLine()) {
String line = s.nextLine();
if (line.equals("")) {
curPair += "\n";
if (!curPair.contains("SHA1-Digest:"))
zos.write(curPair.getBytes());
curPair = "";
} else {
curPair += line + "\n";
}
}
s.close();
// Jar file is different from Zip file. :)
if (f.getName().endsWith(".zip"))
zos.write("\n".getBytes());
zos.closeEntry();
} else {
try {
zos.putNextEntry(outEntry);
InputStream is = zip.getInputStream(e);
byte[] buffer = new byte[1024];
while (true) {
int count = is.read(buffer);
if (count == -1)
break;
zos.write(buffer, 0, count);
}
is.close();
zos.closeEntry();
} catch (ZipException ex) {
if (!ex.getMessage().contains("duplicate entry")) {
ex.printStackTrace();
System.out.println("Ignoring above warning from improper source zip...");
}
}
}
}
}
if (zos != null)
zos.close();
zip.close();
} catch (Exception e) {
System.err.println("Unable to process zip/jar: " + f.getAbsolutePath());
e.printStackTrace();
File dest = new File(outputDir.getPath() + File.separator + f.getName());
FileChannel source = null;
FileChannel destination = null;
try {
source = new FileInputStream(f).getChannel();
destination = new FileOutputStream(dest).getChannel();
destination.transferFrom(source, 0, source.size());
} catch (Exception ex) {
System.err.println("Unable to copy zip/jar: " + f.getAbsolutePath());
ex.printStackTrace();
} finally {
if (source != null) {
try {
source.close();
} catch (IOException e2) {
e2.printStackTrace();
}
}
if (destination != null) {
try {
destination.close();
} catch (IOException e2) {
e2.printStackTrace();
}
}
}
}
}
use of java.util.zip.ZipException in project LibreraReader by foobnix.
the class ZipFile method getInputStream.
/**
* Returns an InputStream for reading the contents of the given entry.
*
* @param ze the entry to get the stream for.
* @return a stream to read the entry from.
* @throws IOException if unable to create an input stream from the zipentry
*/
public InputStream getInputStream(final ZipArchiveEntry ze) throws IOException {
if (!(ze instanceof Entry)) {
return null;
}
// cast validity is checked just above
ZipUtil.checkRequestedFeatures(ze);
final long start = ze.getDataOffset();
// doesn't get closed if the method is not supported - which
// should never happen because of the checkRequestedFeatures
// call above
final InputStream is = // NOSONAR
new BufferedInputStream(createBoundedInputStream(start, ze.getCompressedSize()));
switch(ZipMethod.getMethodByCode(ze.getMethod())) {
case STORED:
return is;
case UNSHRINKING:
return new UnshrinkingInputStream(is);
case IMPLODING:
return new ExplodingInputStream(ze.getGeneralPurposeBit().getSlidingDictionarySize(), ze.getGeneralPurposeBit().getNumberOfShannonFanoTrees(), is);
case DEFLATED:
final Inflater inflater = new Inflater(true);
// https://docs.oracle.com/javase/7/docs/api/java/util/zip/Inflater.html#Inflater(boolean)
return new InflaterInputStream(new SequenceInputStream(is, new ByteArrayInputStream(ONE_ZERO_BYTE)), inflater) {
@Override
public void close() throws IOException {
try {
super.close();
} finally {
inflater.end();
}
}
};
case BZIP2:
return new BZip2CompressorInputStream(is);
case ENHANCED_DEFLATED:
return new Deflate64CompressorInputStream(is);
case AES_ENCRYPTED:
case EXPANDING_LEVEL_1:
case EXPANDING_LEVEL_2:
case EXPANDING_LEVEL_3:
case EXPANDING_LEVEL_4:
case JPEG:
case LZMA:
case PKWARE_IMPLODING:
case PPMD:
case TOKENIZATION:
case UNKNOWN:
case WAVPACK:
case XZ:
default:
throw new ZipException("Found unsupported compression method " + ze.getMethod());
}
}
use of java.util.zip.ZipException in project cdap by caskdata.
the class ArtifactInspector method inspectApplications.
private ArtifactClasses.Builder inspectApplications(Id.Artifact artifactId, ArtifactClasses.Builder builder, Location artifactLocation, ClassLoader artifactClassLoader) throws IOException, InvalidArtifactException {
// right now we force users to include the application main class as an attribute in their manifest,
// which forces them to have a single application class.
// in the future, we may want to let users do this or maybe specify a list of classes or
// a package that will be searched for applications, to allow multiple applications in a single artifact.
String mainClassName;
try {
Manifest manifest = BundleJarUtil.getManifest(artifactLocation);
if (manifest == null) {
return builder;
}
Attributes manifestAttributes = manifest.getMainAttributes();
if (manifestAttributes == null) {
return builder;
}
mainClassName = manifestAttributes.getValue(ManifestFields.MAIN_CLASS);
} catch (ZipException e) {
throw new InvalidArtifactException(String.format("Couldn't unzip artifact %s, please check it is a valid jar file.", artifactId), e);
}
if (mainClassName == null) {
return builder;
}
try {
Object appMain = artifactClassLoader.loadClass(mainClassName).newInstance();
if (!(appMain instanceof Application)) {
// possible for 3rd party plugin artifacts to have the main class set
return builder;
}
Application app = (Application) appMain;
java.lang.reflect.Type configType;
// we can deserialize the config into that object. Otherwise it'll just be a Config
try {
configType = Artifacts.getConfigType(app.getClass());
} catch (Exception e) {
throw new InvalidArtifactException(String.format("Could not resolve config type for Application class %s in artifact %s. " + "The type must extend Config and cannot be parameterized.", mainClassName, artifactId));
}
Schema configSchema = configType == Config.class ? null : schemaGenerator.generate(configType);
builder.addApp(new ApplicationClass(mainClassName, "", configSchema));
} catch (ClassNotFoundException e) {
throw new InvalidArtifactException(String.format("Could not find Application main class %s in artifact %s.", mainClassName, artifactId));
} catch (UnsupportedTypeException e) {
throw new InvalidArtifactException(String.format("Config for Application %s in artifact %s has an unsupported schema. " + "The type must extend Config and cannot be parameterized.", mainClassName, artifactId));
} catch (InstantiationException | IllegalAccessException e) {
throw new InvalidArtifactException(String.format("Could not instantiate Application class %s in artifact %s.", mainClassName, artifactId), e);
}
return builder;
}
use of java.util.zip.ZipException in project MindsEye by SimiaCryptus.
the class SerializationTest method test.
@Nullable
@Override
public ToleranceStatistics test(@Nonnull final NotebookOutput log, @Nonnull final Layer layer, final Tensor... inputPrototype) {
log.h1("Serialization");
log.p("This apply will demonstrate the layer's JSON serialization, and verify deserialization integrity.");
String prettyPrint = "";
log.h2("Raw Json");
try {
prettyPrint = log.code(() -> {
final JsonObject json = layer.getJson();
@Nonnull final Layer echo = Layer.fromJson(json);
if (echo == null)
throw new AssertionError("Failed to deserialize");
if (layer == echo)
throw new AssertionError("Serialization did not copy");
if (!layer.equals(echo))
throw new AssertionError("Serialization not equal");
echo.freeRef();
return new GsonBuilder().setPrettyPrinting().create().toJson(json);
});
@Nonnull String filename = layer.getClass().getSimpleName() + "_" + log.getName() + ".json";
log.p(log.file(prettyPrint, filename, String.format("Wrote Model to %s; %s characters", filename, prettyPrint.length())));
} catch (RuntimeException e) {
e.printStackTrace();
Util.sleep(1000);
} catch (OutOfMemoryError e) {
e.printStackTrace();
Util.sleep(1000);
}
log.p("");
@Nonnull Object outSync = new Object();
if (prettyPrint.isEmpty() || prettyPrint.length() > 1024 * 64)
Arrays.stream(SerialPrecision.values()).parallel().forEach(precision -> {
try {
@Nonnull File file = new File(log.getResourceDir(), log.getName() + "_" + precision.name() + ".zip");
layer.writeZip(file, precision);
@Nonnull final Layer echo = Layer.fromZip(new ZipFile(file));
getModels().put(precision, echo);
synchronized (outSync) {
log.h2(String.format("Zipfile %s", precision.name()));
log.p(log.link(file, String.format("Wrote Model apply %s precision to %s; %.3fMiB bytes", precision, file.getName(), file.length() * 1.0 / (0x100000))));
}
if (!isPersist())
file.delete();
if (echo == null)
throw new AssertionError("Failed to deserialize");
if (layer == echo)
throw new AssertionError("Serialization did not copy");
if (!layer.equals(echo))
throw new AssertionError("Serialization not equal");
} catch (RuntimeException e) {
e.printStackTrace();
} catch (OutOfMemoryError e) {
e.printStackTrace();
} catch (ZipException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
});
return null;
}
Aggregations