Skip to content

Instantly share code, notes, and snippets.

@steklopod
Last active September 12, 2018 12:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save steklopod/4edac4008e41a02e5838e43d8f59ec25 to your computer and use it in GitHub Desktop.
Save steklopod/4edac4008e41a02e5838e43d8f59ec25 to your computer and use it in GitHub Desktop.
Download .rar from http and extract java example
import lombok.extern.slf4j.Slf4j;
import org.apache.camel.Exchange;
import org.apache.commons.lang3.time.StopWatch;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import ru.gamble.servicebus.core.domain.Task;
import ru.gamble.servicebus.core.exception.UnzipFiasException;
import ru.gamble.servicebus.core.metadata.TaskHandlerEnumCore;
import ru.gamble.servicebus.core.model.ifd.TableAndDataSource;
import ru.gamble.servicebus.core.processor.ServiceBusHandler;
import ru.gamble.servicebus.core.processor.ifd.manager.TableWorker;
import ru.gamble.servicebus.core.processor.ifd.service.ContextParamService;
import ru.gamble.servicebus.core.service.SystemSettingsService;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.nio.file.Paths;
import java.util.concurrent.TimeUnit;
import static com.github.junrar.Junrar.extract;
import static java.io.File.separatorChar;
import static java.nio.file.Files.exists;
import static org.springframework.util.FileSystemUtils.deleteRecursively;
@Slf4j
@Service
public class RarExtractor implements ServiceBusHandler {
@Autowired private TableWorker tableWorker;
@Autowired private TableAndDataSource aliasHelper;
@Autowired private SystemSettingsService settingsService;
@Autowired private ContextParamService contextParamService;
private static String PARENT_DIR_NAME = "fias_dbf";
@Override
public void process(Exchange exchange) {
StopWatch stopwatch = new StopWatch();
stopwatch.start();
try {
URL url = new URL(aliasHelper.getMethodValueAsFullURL(exchange));
if (parseArchiveType(url).equals(".rar")) {
String pathToSave = getFullPathToSaveFromTTParam(exchange);
addPathToGlobalContext(exchange, pathToSave);
File rar = saveRar(url, pathToSave);
extract(rar, new File(pathToSave));
deleteRecursively(rar);
logWork(stopwatch, pathToSave);
} else { throw new UnsupportedOperationException("The type of archive is unsupported now. Only `rar` type supported."); }
}
catch (Exception e) {
log.error("Error when unzipping file UnzipHandler.", e.getCause());
throw new UnzipFiasException(e);
}
}
private void addPathToGlobalContext(Exchange exchange, String pathToSave) throws IOException {
contextParamService.putVariableToContext(
getTask(exchange).getTaskParam(),
FiasUrils.FIAS_FILES_FOLDER,
pathToSave,
true
);
}
private String getFileNameForSaving(URL url) {
String urlAsStr = url.toString();
return urlAsStr.substring(urlAsStr.lastIndexOf("/") + 1);
}
private File saveRar(URL urlFrom, String pathToSave) {
log.info("\n \n \n >>> Getting resource from: " + urlFrom + "\n >>> Saving into: " + pathToSave + "\n >>>Please, be passion, this may take about 15 minutes. \n \n \n");
makeDirIfNotExists(pathToSave);
File rarFile = new File(pathToSave + separatorChar + getFileNameForSaving(urlFrom));
try (ReadableByteChannel rbc = Channels.newChannel(urlFrom.openStream());
FileOutputStream fos = new FileOutputStream(rarFile))
{
fos.getChannel().transferFrom(rbc, 0L, Long.MAX_VALUE);
}
catch (Exception e) { log.error(String.valueOf(e.getCause()), e); throw new UnzipFiasException("Error when trying to save zip from " + urlFrom + "\n" + e.getMessage()); }
return rarFile;
}
private String getFullPathToSaveFromTTParam(Exchange exchange) {
getparentDirName();
String pathToSave = tableWorker.getTTParamAsString("PATH_TO_SAVE", exchange)
.replace("/", String.valueOf(separatorChar))
.replace("\\", String.valueOf(separatorChar));
Task task = exchange.getIn().getBody(Task.class);
Long taskId = task.getId();
String fullPath = addSeparatorToTheEnd(pathToSave)
+ PARENT_DIR_NAME
+ separatorChar + taskId;
task.getTaskParam().setResponseBody(fullPath);
return fullPath;
}
/**
* Перепроверить таблицу [systemsettings] на наличие записи с `code` = task_files.parent.folder.
*/
private void getparentDirName() {
try{
PARENT_DIR_NAME = settingsService.getByCode("task_files.parent.folder").getValue();
}catch (NullPointerException e){
log.warn("Please, add parent directory name in [systemsettings] table with field `code`, that must have value = `task_files.parent.folder`" +
"Default name [{}] was used for package name", PARENT_DIR_NAME);
}
}
private String parseArchiveType(URL url) {
return "." + (url.getPath().substring(url.getPath().lastIndexOf(".") + 1)).trim().toLowerCase();
}
private String addSeparatorToTheEnd(String pathToSave) {
if (!pathToSave.endsWith(File.separator)) pathToSave += separatorChar;
return pathToSave;
}
private void makeDirIfNotExists(String path) {
File dir = new File(path);
if (exists(Paths.get(path))) deleteRecursively(dir);
dir.mkdirs();
}
private void logWork(StopWatch stopwatch, String pathToSave) {
log.info("Ok. Archive successfully extracted into {}", pathToSave);
stopwatch.stop();
long elapsedTime = stopwatch.getTime();
log.info(String.format("Время выполнения: %02d минут, %02d секунд",
TimeUnit.MILLISECONDS.toMinutes(elapsedTime),
TimeUnit.MILLISECONDS.toSeconds(elapsedTime) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(elapsedTime))
));
}
@Override public String getTaskHandler() { return TaskHandlerEnumCore.UNZIP.name(); }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment