Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

changed handling of zip directories #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
248 changes: 127 additions & 121 deletions src/main/java/io/vertx/mods/UnzipVerticle.java
Original file line number Diff line number Diff line change
@@ -1,121 +1,127 @@
package io.vertx.mods;

import org.vertx.java.core.Handler;
import org.vertx.java.core.eventbus.Message;
import org.vertx.java.core.json.JsonObject;
import org.vertx.java.platform.PlatformManagerException;
import org.vertx.java.platform.Verticle;

import java.io.*;
import java.util.UUID;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

/*
* Copyright 2013 Red Hat, Inc.
*
* Red Hat licenses this file to you under the Apache License, version 2.0
* (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* @author <a href="http://tfox.org">Tim Fox</a>
*/
public class UnzipVerticle extends Verticle {

private static final String TEMP_DIR = System.getProperty("java.io.tmpdir");
private static final String FILE_SEP = System.getProperty("file.separator");
private static final int BUFFER_SIZE = 4096;

@Override
public void start() {

JsonObject conf = container.config();
String address = conf.getString("address", "io.vertx.unzipper");

vertx.eventBus().registerHandler(address, new Handler<Message<JsonObject>>() {
@Override
public void handle(Message<JsonObject> message) {
String zipFile = message.body().getString("zipFile");
if (zipFile == null) {
sendError("Please specify zipFile field in message", message);
return;
}
String destDir = message.body().getString("destDir");
if (destDir == null) {
// Generate a tmp dest dir
destDir = generateTmpFileName();
}
File dest = new File(destDir);
if (!dest.exists()) {
if (!dest.mkdir()) {
sendError("Failed to create directory " + dest, message);
return;
}
}
boolean deleteZip = message.body().getBoolean("deleteZip", false);
try {
unzipModuleData(dest, zipFile);
if (deleteZip) {
if (!new File(zipFile).delete()) {
sendError("Failed to delete zip file " + dest, message);
return;
}
}
message.reply(new JsonObject().putString("status", "ok").putString("destDir", destDir));
} catch (Exception e) {
sendError("Failed to unzip module: " + e.getMessage(), message);
}
}
});
}

private void sendError(String errMsg, Message<JsonObject> msg) {
JsonObject reply = new JsonObject().putString("status", "error").putString("message", errMsg);
msg.reply(reply);
}

private String generateTmpFileName() {
return TEMP_DIR + FILE_SEP + "vertx-" + UUID.randomUUID().toString();
}

private void unzipModuleData(final File directory, final String zipFileName) throws Exception {
try (InputStream is = new BufferedInputStream(new FileInputStream(zipFileName));
ZipInputStream zis = new ZipInputStream(new BufferedInputStream(is))) {
ZipEntry entry;
while ((entry = zis.getNextEntry()) != null) {
String entryName = entry.getName();
if (!entryName.isEmpty()) {
if (entry.isDirectory()) {
if (!new File(directory, entryName).mkdir()) {
throw new PlatformManagerException("Failed to create directory");
}
} else {
int count;
byte[] buff = new byte[BUFFER_SIZE];
BufferedOutputStream dest = null;
try {
OutputStream fos = new FileOutputStream(new File(directory, entryName));
dest = new BufferedOutputStream(fos, BUFFER_SIZE);
while ((count = zis.read(buff, 0, BUFFER_SIZE)) != -1) {
dest.write(buff, 0, count);
}
dest.flush();
} finally {
if (dest != null) {
dest.close();
}
}
}
}
}
}
}
}
package io.vertx.mods;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.UUID;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import org.vertx.java.core.Handler;
import org.vertx.java.core.eventbus.Message;
import org.vertx.java.core.json.JsonObject;
import org.vertx.java.platform.Verticle;

/*
* Copyright 2013 Red Hat, Inc.
*
* Red Hat licenses this file to you under the Apache License, version 2.0
* (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* @author <a href="http://tfox.org">Tim Fox</a>
*/
public class UnzipVerticle extends Verticle {

private static final String TEMP_DIR = System.getProperty("java.io.tmpdir");
private static final String FILE_SEP = System.getProperty("file.separator");
private static final int BUFFER_SIZE = 4096;

@Override
public void start() {

JsonObject conf = container.config();
String address = conf.getString("address", "io.vertx.unzipper");

vertx.eventBus().registerHandler(address, new Handler<Message<JsonObject>>() {
@Override
public void handle(Message<JsonObject> message) {
String zipFile = message.body().getString("zipFile");
if (zipFile == null) {
sendError("Please specify zipFile field in message", message);
return;
}
String destDir = message.body().getString("destDir");
if (destDir == null) {
// Generate a tmp dest dir
destDir = generateTmpFileName();
}

Path dest;
try {
dest = Files.createDirectories(Paths.get(destDir));
} catch (Exception e) {
sendError("Failed to create directory " + destDir + " (" + e.getMessage() + ")", message);
return;
}
try {
unzipData(dest.toString(), zipFile);
} catch (Exception e) {
sendError("Failed to unzip file: " + destDir + " (" + e.getMessage() + ")", message);
return;
}
try {
boolean deleteZip = message.body().getBoolean("deleteZip", false);
if (deleteZip)
Files.delete(Paths.get(zipFile));
} catch (Exception e) {
sendError("Failed to delete zip file " + destDir + " (" + e.getMessage() + ")", message);
return;
}
message.reply(new JsonObject().putString("status", "ok").putString("destDir", destDir));
}
});
}

private void sendError(String errMsg, Message<JsonObject> msg) {
JsonObject reply = new JsonObject().putString("status", "error").putString("message", errMsg);
msg.reply(reply);
}

private String generateTmpFileName() {
return TEMP_DIR + FILE_SEP + "vertx-" + UUID.randomUUID().toString();
}

private void unzipData(final String directory, final String zipFileName) throws Exception {
try (InputStream is = new BufferedInputStream(new FileInputStream(zipFileName));
ZipInputStream zis = new ZipInputStream(new BufferedInputStream(is))) {
ZipEntry entry;
while ((entry = zis.getNextEntry()) != null) {
String entryName = entry.getName();
if (!entryName.isEmpty()) {
if (entry.isDirectory()) {
Files.createDirectories(Paths.get(directory, entryName));
continue;
}
Files.createDirectories(Paths.get(directory, entryName).getParent());

int count;
byte[] buff = new byte[BUFFER_SIZE];
try (OutputStream fos = new FileOutputStream(Paths.get(directory, entryName).toFile());
BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER_SIZE);) {

while ((count = zis.read(buff, 0, BUFFER_SIZE)) != -1) {
dest.write(buff, 0, count);
}
dest.flush();
} catch (Exception e) {
}
}
}
}
}
}
Loading