Search in sources :

Example 81 with ZipInputStream

use of java.util.zip.ZipInputStream in project gocd by gocd.

the class TestUtils method extractZipToDir.

public static void extractZipToDir(File repoZip, File repoDir) throws IOException {
    ZipInputStream is = null;
    try {
        is = new ZipInputStream(new FileInputStream(repoZip));
        ZipEntry entry = null;
        while ((entry = is.getNextEntry()) != null) {
            if (entry.isDirectory()) {
                FileUtils.forceMkdir(new File(repoDir, entry.getName()));
            } else {
                File file = new File(repoDir, entry.getName());
                FileUtils.forceMkdir(file.getParentFile());
                FileOutputStream entryOs = null;
                try {
                    entryOs = new FileOutputStream(file);
                    IOUtils.copy(is, entryOs);
                } finally {
                    if (entryOs != null) {
                        entryOs.close();
                    }
                }
            }
        }
    } finally {
        if (is != null) {
            is.close();
        }
    }
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) ZipEntry(java.util.zip.ZipEntry)

Example 82 with ZipInputStream

use of java.util.zip.ZipInputStream in project felix by apache.

the class DataModelHelperImpl method repository.

public Repository repository(final URL url) throws Exception {
    InputStream is = null;
    try {
        if (url.getPath().endsWith(".zip")) {
            ZipInputStream zin = new ZipInputStream(FileUtil.openURL(url));
            ZipEntry entry = zin.getNextEntry();
            while (entry != null) {
                if (entry.getName().equals("repository.xml") || entry.getName().equals("index.xml")) {
                    is = zin;
                    break;
                }
                entry = zin.getNextEntry();
            }
            // as the ZipInputStream is not used further it would not be closed.
            if (is == null) {
                try {
                    zin.close();
                } catch (IOException ex) {
                // Not much we can do.
                }
            }
        } else if (url.getPath().endsWith(".gz")) {
            is = new GZIPInputStream(FileUtil.openURL(url));
        } else {
            is = FileUtil.openURL(url);
        }
        if (is != null) {
            String repositoryUri = url.toExternalForm();
            String baseUri;
            if (repositoryUri.endsWith(".zip")) {
                baseUri = new StringBuilder("jar:").append(repositoryUri).append("!/").toString();
            } else if (repositoryUri.endsWith(".xml")) {
                baseUri = repositoryUri.substring(0, repositoryUri.lastIndexOf('/') + 1);
            } else {
                baseUri = repositoryUri;
            }
            RepositoryImpl repository = repository(is, URI.create(baseUri));
            repository.setURI(repositoryUri);
            return repository;
        } else {
            // This should not happen.
            throw new Exception("Unable to get input stream for repository.");
        }
    } finally {
        try {
            if (is != null) {
                is.close();
            }
        } catch (IOException ex) {
        // Not much we can do.
        }
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) ZipInputStream(java.util.zip.ZipInputStream) GZIPInputStream(java.util.zip.GZIPInputStream) ZipInputStream(java.util.zip.ZipInputStream) ZipEntry(java.util.zip.ZipEntry) URISyntaxException(java.net.URISyntaxException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException)

Example 83 with ZipInputStream

use of java.util.zip.ZipInputStream in project gocd by gocd.

the class FelixGoPluginOSGiFrameworkIntegrationTest method setUp.

@Before
public void setUp() throws Exception {
    TMP_DIR = new File("./tmp" + RANDOM.nextFloat());
    recreateDirectory(TMP_DIR);
    registry = new DefaultPluginRegistry();
    pluginOSGiFramework = new FelixGoPluginOSGiFramework(registry, new SystemEnvironment());
    pluginOSGiFramework.start();
    try (ZipInputStream zippedOSGiBundleFile = new ZipInputStream(FileUtils.openInputStream(pathOfFileInDefaultFiles("descriptor-aware-test-plugin.osgi.jar")))) {
        descriptorBundleDir = explodeBundleIntoDirectory(zippedOSGiBundleFile, "descriptor-plugin-bundle-dir");
    }
    try (ZipInputStream zippedOSGiBundleFile = new ZipInputStream(FileUtils.openInputStream(pathOfFileInDefaultFiles("error-generating-descriptor-aware-test-plugin.osgi.jar")))) {
        errorGeneratingDescriptorBundleDir = explodeBundleIntoDirectory(zippedOSGiBundleFile, "error-generating-descriptor-plugin-bundle-dir");
    }
    try (ZipInputStream zippedOSGiBundleFile = new ZipInputStream(FileUtils.openInputStream(pathOfFileInDefaultFiles("exception-throwing-at-load-plugin.osgi.jar")))) {
        exceptionThrowingAtLoadDescriptorBundleDir = explodeBundleIntoDirectory(zippedOSGiBundleFile, "exception-throwing-at-load-plugin-bundle-dir");
    }
    try (ZipInputStream zippedOSGiBundleFile = new ZipInputStream(FileUtils.openInputStream(pathOfFileInDefaultFiles("valid-plugin-with-multiple-extensions.osgi.jar")))) {
        validMultipleExtensionPluginBundleDir = explodeBundleIntoDirectory(zippedOSGiBundleFile, "valid-plugin-with-multiple-extensions");
    }
}
Also used : SystemEnvironment(com.thoughtworks.go.util.SystemEnvironment) ZipInputStream(java.util.zip.ZipInputStream) DefaultPluginRegistry(com.thoughtworks.go.plugin.infra.plugininfo.DefaultPluginRegistry) File(java.io.File) Before(org.junit.Before)

Example 84 with ZipInputStream

use of java.util.zip.ZipInputStream in project openremote by openremote.

the class EtsFileUriResolver method resolve.

@Override
public Source resolve(String href, String base) throws TransformerException {
    ZipInputStream zin = new ZipInputStream(new ByteArrayInputStream(data));
    ZipEntry zipEntry = null;
    try {
        zipEntry = zin.getNextEntry();
        String entryData = null;
        while (zipEntry != null) {
            if (zipEntry.getName().equals(href)) {
                entryData = convertStreamToString(zin);
                break;
            }
            zipEntry = zin.getNextEntry();
        }
        return new StreamSource(new ByteArrayInputStream(entryData.getBytes()));
    } catch (IOException e) {
        LOG.log(Level.WARNING, "Could not create XML Stream Source for '" + href + "' from ETS project file.");
    }
    return null;
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ZipEntry(java.util.zip.ZipEntry) StreamSource(javax.xml.transform.stream.StreamSource) IOException(java.io.IOException)

Example 85 with ZipInputStream

use of java.util.zip.ZipInputStream in project openremote by openremote.

the class KNXProtocol method discoverLinkedAssetAttributes.

@Override
public Asset[] discoverLinkedAssetAttributes(AssetAttribute protocolConfiguration, FileInfo fileInfo) throws IllegalStateException {
    ZipInputStream zin = null;
    try {
        boolean fileFound = false;
        byte[] data = CodecUtil.decodeBase64(fileInfo.getContents());
        zin = new ZipInputStream(new ByteArrayInputStream(data));
        ZipEntry zipEntry = zin.getNextEntry();
        while (zipEntry != null) {
            if (zipEntry.getName().endsWith("/0.xml")) {
                fileFound = true;
                break;
            }
            zipEntry = zin.getNextEntry();
        }
        if (!fileFound) {
            String msg = "Failed to find '0.xml' in project file";
            LOG.info(msg);
            throw new IllegalStateException(msg);
        }
        // Create a transform factory instance.
        System.setProperty("javax.xml.transform.TransformerFactory", "net.sf.saxon.TransformerFactoryImpl");
        TransformerFactory tfactory = TransformerFactory.newInstance();
        // Create a transformer for the stylesheet.
        Transformer transformer = tfactory.newTransformer(new StreamSource(this.getClass().getResourceAsStream("/org/openremote/agent/protocol/knx/ets_calimero_group_name.xsl")));
        // Set the URIResolver
        transformer.setURIResolver(new EtsFileUriResolver(data));
        // Transform the source XML into byte array
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        transformer.transform(new StreamSource(zin), new StreamResult(bos));
        byte[] result = bos.toByteArray();
        // we use a map of state-based datapoints and read from the transformed xml
        final DatapointMap<StateDP> datapoints = new DatapointMap<>();
        try (final XmlReader r = XmlInputFactory.newInstance().createXMLStreamReader(new ByteArrayInputStream(result))) {
            datapoints.load(r);
        } catch (final KNXMLException e) {
            String msg = "Error loading parsed ETS file: " + e.getMessage();
            LOG.warning(msg);
            throw new IllegalStateException(msg, e);
        }
        MetaItem agentLink = AgentLink.asAgentLinkMetaItem(protocolConfiguration.getReferenceOrThrow());
        Map<String, Asset> createdAssets = new HashMap<>();
        for (StateDP dp : datapoints.getDatapoints()) {
            if (dp.getName().endsWith("#A")) {
                createAsset(dp, false, agentLink, createdAssets);
            } else if (dp.getName().endsWith("#S")) {
                createAsset(dp, true, agentLink, createdAssets);
            } else if (dp.getName().endsWith("#SA") || dp.getName().endsWith("#AS")) {
                createAsset(dp, false, agentLink, createdAssets);
                createAsset(dp, true, agentLink, createdAssets);
            } else {
                LOG.info("Only group addresses ending on #A, #S, #AS or #SA will be imported. Ignoring: " + dp.getName());
            }
        }
        return createdAssets.values().toArray(new Asset[createdAssets.values().size()]);
    } catch (Exception e) {
        throw new IllegalStateException("ETS import error", e);
    } finally {
        if (zin != null) {
            try {
                zin.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
Also used : TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) DatapointMap(tuwien.auto.calimero.datapoint.DatapointMap) StreamResult(javax.xml.transform.stream.StreamResult) ZipEntry(java.util.zip.ZipEntry) StreamSource(javax.xml.transform.stream.StreamSource) ByteArrayOutputStream(java.io.ByteArrayOutputStream) XmlReader(tuwien.auto.calimero.xml.XmlReader) KNXMLException(tuwien.auto.calimero.xml.KNXMLException) IOException(java.io.IOException) KNXMLException(tuwien.auto.calimero.xml.KNXMLException) KNXFormatException(tuwien.auto.calimero.KNXFormatException) IOException(java.io.IOException) KNXException(tuwien.auto.calimero.KNXException) ZipInputStream(java.util.zip.ZipInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) StateDP(tuwien.auto.calimero.datapoint.StateDP) Asset(org.openremote.model.asset.Asset)

Aggregations

ZipInputStream (java.util.zip.ZipInputStream)968 ZipEntry (java.util.zip.ZipEntry)762 IOException (java.io.IOException)355 File (java.io.File)319 FileInputStream (java.io.FileInputStream)316 InputStream (java.io.InputStream)203 FileOutputStream (java.io.FileOutputStream)198 ByteArrayInputStream (java.io.ByteArrayInputStream)190 ByteArrayOutputStream (java.io.ByteArrayOutputStream)138 BufferedInputStream (java.io.BufferedInputStream)127 ZipOutputStream (java.util.zip.ZipOutputStream)91 Test (org.junit.Test)89 ArrayList (java.util.ArrayList)80 OutputStream (java.io.OutputStream)67 URL (java.net.URL)58 Path (java.nio.file.Path)58 FileNotFoundException (java.io.FileNotFoundException)56 HashMap (java.util.HashMap)56 BufferedOutputStream (java.io.BufferedOutputStream)54 ZipFile (java.util.zip.ZipFile)43