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();
}
}
}
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.
}
}
}
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");
}
}
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;
}
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();
}
}
}
}
Aggregations