Example usage for java.util.zip ZipException printStackTrace

List of usage examples for java.util.zip ZipException printStackTrace

Introduction

In this page you can find the example usage for java.util.zip ZipException printStackTrace.

Prototype

public void printStackTrace() 

Source Link

Document

Prints this throwable and its backtrace to the standard error stream.

Usage

From source file:org.kie.guvnor.m2repo.backend.server.GuvnorM2Repository.java

public File appendPOMToJar(String pom, String jarPath, GAV gav) {
    File originalJarFile = new File(jarPath);
    File appendedJarFile = new File(jarPath + ".tmp");

    try {/*from  ww  w.j  ava 2s.c  o  m*/
        ZipFile war = new ZipFile(originalJarFile);

        ZipOutputStream append = new ZipOutputStream(new FileOutputStream(appendedJarFile));

        // first, copy contents from existing war
        Enumeration<? extends ZipEntry> entries = war.entries();
        while (entries.hasMoreElements()) {
            ZipEntry e = entries.nextElement();
            // System.out.println("copy: " + e.getName());
            append.putNextEntry(e);
            if (!e.isDirectory()) {
                IOUtil.copy(war.getInputStream(e), append);
            }
            append.closeEntry();
        }

        // append pom.
        ZipEntry e = new ZipEntry(getPomXmlPath(gav));
        // System.out.println("append: " + e.getName());
        append.putNextEntry(e);
        append.write(pom.getBytes());
        append.closeEntry();

        // close
        war.close();
        append.close();
    } catch (ZipException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    //originalJarFile.delete();
    //appendedJarFile.renameTo(originalJarFile);
    return appendedJarFile;
}

From source file:pl.asie.modalyze.ModAnalyzer.java

public ModMetadata analyze(ZipInputStream stream) {
    List<ModMetadata> recursiveMods = new ArrayList<>();
    ModMetadata metadata = new ModMetadata();
    if (isVerbose) {
        System.err.println("[*] " + file.toString());
    }// w  w  w. jav a2 s  .c o m

    try {
        ZipEntry entry;
        while ((entry = stream.getNextEntry()) != null) {
            if (entry.getName().equals("mcmod.info")) {
                appendMcmodInfo(metadata, stream);
            } else if (entry.getName().endsWith(".class")) {
                appendClassInfo(metadata, stream);
            } else if (entry.getName().endsWith(".zip") || entry.getName().endsWith(".jar")) {
                ModMetadata meta = Main.analyzer(null).analyze(new ZipInputStream(stream));
                if (meta != null && meta.valid) {
                    recursiveMods.add(meta);
                }
            } else if (entry.getName().equals("META-INF/MANIFEST.MF")) {
                appendManifest(metadata, stream);
            }
        }
    } catch (ZipException exception) {
        return null;
    } catch (IOException exception) {
        exception.printStackTrace();
        return null;
    }

    if (!metadata.valid) {
        if (recursiveMods.size() == 1) {
            metadata = recursiveMods.get(0);
        } else if (recursiveMods.size() == 2 && recursiveMods.get(0).modid != null
                && recursiveMods.get(1).modid != null
                && recursiveMods.get(0).modid.equals(recursiveMods.get(1).modid)
                && ((recursiveMods.get(0).side.equals("client") && recursiveMods.get(1).side.equals("server"))
                        || (recursiveMods.get(1).side.equals("client")
                                && recursiveMods.get(0).side.equals("server")))) {
            metadata = recursiveMods.get(0);
            metadata.side = "universal";
        }
    }

    if (metadata.provides != null) {
        metadata.provides.remove(metadata.modid);
        if (metadata.provides.size() == 0) {
            metadata.provides = null;
        } else {
            metadata.valid = true;
        }
    }

    if (metadata.dependencies != null) {
        metadata.dependencies.remove(metadata.modid);
        if (metadata.provides != null) {
            for (String id : metadata.provides) {
                metadata.dependencies.remove(id);
            }
        }
        if (metadata.dependencies.size() == 0) {
            metadata.dependencies = null;
        } else {
            metadata.valid = true;
        }
    }

    if (metadata.side == null) {
        if (metadata.dependencies != null && metadata.dependencies.containsKey("minecraft")
                && !metadata.dependencies.get("minecraft").equals("*")) {
            boolean hasSides = MCP.hasSides(metadata.dependencies.get("minecraft"));
            if (!hasSides) {
                metadata.side = "universal";
            }
        }
    }

    if (versionHeuristics) {
        if (metadata.side == null || metadata.dependencies == null
                || !metadata.dependencies.containsKey("minecraft")
                || metadata.dependencies.get("minecraft").equals("*")) {
            Set<String> versions = new HashSet<>();
            String version;
            boolean hasClient = false, hasServer = false;
            Collection<String> heuristicVersions = MCP.getVersionsForKeySet(keys);
            if (heuristicVersions != null) {
                for (String s : heuristicVersions) {
                    if (s.endsWith("-client")) {
                        hasClient = true;
                    } else if (s.endsWith("-server")) {
                        hasServer = true;
                    }
                    versions.add(s.split("-")[0]);
                }

                if (versions.size() == 1) {
                    version = (String) versions.toArray()[0];
                } else {
                    version = Arrays.toString(versions.toArray(new String[versions.size()]));
                    version = version.replace('[', '{');
                    version = version.replace(']', '}');
                }

                boolean hasSides = false;
                for (String s : versions) {
                    if (MCP.hasSides(s)) {
                        hasSides = true;
                        break;
                    }
                }

                String side = (!hasSides || hasClient == hasServer) ? "universal"
                        : (hasClient ? "client" : "server");
                metadata.valid = true;
                metadata.side = side;
                metadata.dependencies = addDependency(metadata.dependencies, "minecraft@" + version);
            }
        }
    }

    if (generateHash) {
        try {
            metadata.sha256 = DigestUtils.sha256Hex(new FileInputStream(file));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    if (storeFilenames) {
        metadata.filename = file.getName();
    }

    return metadata;
}

From source file:nova.core.render.model.TechneModelProvider.java

@Override
public void load(InputStream stream) {
    try {/*from   w w w .java  2  s. c om*/
        Map<String, byte[]> zipContents = new HashMap<>();
        ZipInputStream zipInput = new ZipInputStream(stream);
        ZipEntry entry;
        while ((entry = zipInput.getNextEntry()) != null) {
            byte[] data = new byte[(int) entry.getSize()];
            // For some reason, using read(byte[]) makes reading stall upon reaching a 0x1E byte
            int i = 0;
            while (zipInput.available() > 0 && i < data.length) {
                data[i++] = (byte) zipInput.read();
            }
            zipContents.put(entry.getName(), data);
        }

        byte[] modelXml = zipContents.get("model.xml");
        if (modelXml == null) {
            throw new RenderException("Model " + name + " contains no model.xml file");
        }

        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
        Document document = documentBuilder.parse(new ByteArrayInputStream(modelXml));

        NodeList nodeListTechne = document.getElementsByTagName("Techne");
        if (nodeListTechne.getLength() < 1) {
            throw new RenderException("Model " + name + " contains no Techne tag");
        }

        NodeList nodeListModel = document.getElementsByTagName("Model");
        if (nodeListModel.getLength() < 1) {
            throw new RenderException("Model " + name + " contains no Model tag");
        }

        NamedNodeMap modelAttributes = nodeListModel.item(0).getAttributes();
        if (modelAttributes == null) {
            throw new RenderException("Model " + name + " contains a Model tag with no attributes");
        }

        NodeList textureSize = document.getElementsByTagName("TextureSize");
        if (textureSize.getLength() == 0)
            throw new RenderException("Model has no texture size");

        String[] textureDimensions = textureSize.item(0).getTextContent().split(",");
        double textureWidth = Integer.parseInt(textureDimensions[0]);
        double textureHeight = Integer.parseInt(textureDimensions[1]);

        NodeList shapes = document.getElementsByTagName("Shape");

        for (int i = 0; i < shapes.getLength(); i++) {
            Node shape = shapes.item(i);
            NamedNodeMap shapeAttributes = shape.getAttributes();
            if (shapeAttributes == null) {
                throw new RenderException("Shape #" + (i + 1) + " in " + name + " has no attributes");
            }

            Node name = shapeAttributes.getNamedItem("name");
            String shapeName = null;
            if (name != null) {
                shapeName = name.getNodeValue();
            }
            if (shapeName == null) {
                shapeName = "Shape #" + (i + 1);
            }

            String shapeType = null;
            Node type = shapeAttributes.getNamedItem("type");
            if (type != null) {
                shapeType = type.getNodeValue();
            }

            if (shapeType != null && !cubeIDs.contains(shapeType)) {
                System.out.println(
                        "Model shape [" + shapeName + "] in " + this.name + " is not a cube, ignoring");
                continue;
            }

            boolean mirrored = false;
            String[] offset = new String[3];
            String[] position = new String[3];
            String[] rotation = new String[3];
            String[] size = new String[3];
            String[] textureOffset = new String[2];

            NodeList shapeChildren = shape.getChildNodes();
            for (int j = 0; j < shapeChildren.getLength(); j++) {
                Node shapeChild = shapeChildren.item(j);

                String shapeChildName = shapeChild.getNodeName();
                String shapeChildValue = shapeChild.getTextContent();
                if (shapeChildValue != null) {
                    shapeChildValue = shapeChildValue.trim();

                    switch (shapeChildName) {
                    case "IsMirrored":
                        mirrored = !shapeChildValue.equals("False");
                        break;
                    case "Offset":
                        offset = shapeChildValue.split(",");
                        break;
                    case "Position":
                        position = shapeChildValue.split(",");
                        break;
                    case "Rotation":
                        rotation = shapeChildValue.split(",");
                        break;
                    case "Size":
                        size = shapeChildValue.split(",");
                        break;
                    case "TextureOffset":
                        textureOffset = shapeChildValue.split(",");
                        break;
                    }
                }
            }

            /*
                 Generate new models
                 Models in Techne are based on cubes.
                 Each cube is, by default, skewed to the side. They are not centered.
                    
                 Everything is scaled by a factor of 16.
                 The y coordinate is inversed, y = 24 is the surface
                 The z coordinate is inverted, too.
             */
            double positionX = Double.parseDouble(position[0]) / 16d;
            double positionY = (16 - Double.parseDouble(position[1])) / 16d;
            double positionZ = -Double.parseDouble(position[2]) / 16d;

            double sizeX = Double.parseDouble(size[0]) / 16d;
            double sizeY = Double.parseDouble(size[1]) / 16d;
            double sizeZ = Double.parseDouble(size[2]) / 16d;

            double offsetX = Double.parseDouble(offset[0]) / 16d;
            double offsetY = -Double.parseDouble(offset[1]) / 16d;
            double offsetZ = -Double.parseDouble(offset[2]) / 16d;

            double angleX = -Math.toRadians(Double.parseDouble(rotation[0]));
            double angleY = Math.toRadians(Double.parseDouble(rotation[1]));
            double angleZ = Math.toRadians(Double.parseDouble(rotation[2]));

            double textureOffsetU = Double.parseDouble(textureOffset[0]);
            double textureOffsetV = Double.parseDouble(textureOffset[1]);

            CubeTextureCoordinates textureCoordinates = new TechneCubeTextureCoordinates(textureWidth,
                    textureHeight, textureOffsetU, textureOffsetV, sizeX, sizeY, sizeZ);

            final String modelName = shapeName;
            MeshModel modelPart = new MeshModel(modelName);
            BlockRenderPipeline.drawCube(modelPart, offsetX, offsetY - sizeY, offsetZ - sizeZ, offsetX + sizeX,
                    offsetY, offsetZ, textureCoordinates);

            MatrixStack ms = new MatrixStack();
            ms.translate(positionX, positionY, positionZ);
            ms.rotate(Vector3D.PLUS_J, angleY);
            ms.rotate(Vector3D.PLUS_I, angleX);
            ms.rotate(Vector3D.PLUS_K, angleZ);
            modelPart.matrix = ms;
            modelPart.textureOffset = new Vector2D(Integer.parseInt(textureOffset[0]),
                    Integer.parseInt(textureOffset[1]));

            if (model.children.stream().anyMatch(m -> m.name.equals(modelName))) {
                throw new RenderException(
                        "Model contained duplicate part name: '" + shapeName + "' node #" + i);
            }

            model.children.add(modelPart);
        }
    } catch (ZipException e) {
        throw new RenderException("Model " + name + " is not a valid zip file");
    } catch (IOException e) {
        throw new RenderException("Model " + name + " could not be read", e);
    } catch (SAXException e) {
        throw new RenderException("Model " + name + " contains invalid XML", e);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:edu.ku.brc.specify.config.ResourceImportExportDlg.java

/**
 * @param file/*from w  w w  . ja v  a2 s  . c  o  m*/
 * @return name of report resource contained in file, or null if file does not
 * contain a report resource.
 */
protected String getSpReportResourceName(final File file) {
    try {
        ZipInputStream zin = new ZipInputStream(new FileInputStream(file));
        ZipEntry app = zin.getNextEntry();
        if (app == null) {
            return null;
        }
        if (zin.available() == 0) {
            return null;
        }
        String appStr = readZipEntryToString(zin, app);
        if (isSpReportResource(appStr)) {
            Element appElement = XMLHelper.readStrToDOM4J(appStr);
            return XMLHelper.getAttr(appElement, "name", null);
        }
        return null;
    } catch (ZipException ex) {
        //I think this means it is not a zip file.
        return null;
    } catch (Exception ex) {
        ex.printStackTrace();
        edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount();
        edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(ResourceImportExportDlg.class, ex);
        return null;
    }
}

From source file:edu.harvard.iq.dvn.core.web.subsetting.AnalysisPage.java

public void zipFiles(OutputStream out, List<File> fllst) {
    ZipOutputStream zout = null;/*from  ww w.j  a va2  s  .  co m*/
    //BufferedInputStream infile = null;
    FileInputStream infile = null;

    zout = new ZipOutputStream(out);

    for (int i = 0; i < fllst.size(); i++) {
        try {
            infile = new FileInputStream(fllst.get(i));//new BufferedInputStream()

        } catch (FileNotFoundException e) {
            err.println("input file is not found");
            e.printStackTrace();
            try {
                zout.close();
            } catch (ZipException ze) {
                err.println("zip file invalid");
                ze.printStackTrace();
            } catch (IOException ex) {
                err.println("closing output file");
                ex.printStackTrace();
            }
        }

        ZipEntry ze = new ZipEntry(fllst.get(i).getName());
        try {
            zout.putNextEntry(ze);
            /*
                int len;
                while ((len = infile.read())> 0) {
            zout.write(len);
                }
            */

            byte[] dataBuffer = new byte[8192];

            int k = 0;
            while ((k = infile.read(dataBuffer)) > 0) {
                zout.write(dataBuffer, 0, k);
                //fileSize += i; 
                out.flush();
            }

        } catch (ZipException zpe) {
            zpe.printStackTrace();
            err.println("zip file is invalid");
        } catch (IOException ie) {
            ie.printStackTrace();
            err.println("output file io-error");
        }

        try {
            infile.close();
        } catch (IOException ie) {
            err.println("error: closing input file");
        }
    }

    try {
        zout.close();
    } catch (ZipException zpe) {
        err.println("zip file invalid");
        zpe.printStackTrace();
    } catch (IOException ioe) {
        err.println("error closing zip file");
        ioe.printStackTrace();
    }

}