Zip standart lib implementations
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
package me.bearns.fias.exceptions;
|
||||
|
||||
public abstract class CommonException extends Exception {
|
||||
public CommonException(Exception e) {
|
||||
super(e);
|
||||
}
|
||||
|
||||
public CommonException(String s) {
|
||||
super(s);
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
package me.bearns.fias.exceptions;
|
||||
|
||||
public class DownloadException extends Exception {
|
||||
public class DownloadException extends CommonException {
|
||||
public DownloadException(Exception e) {
|
||||
super(e);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package me.bearns.fias.exceptions;
|
||||
|
||||
public class UnzipException extends Exception {
|
||||
public class UnzipException extends CommonException {
|
||||
public UnzipException(Exception e){
|
||||
super(e);
|
||||
}
|
||||
|
||||
@@ -1,20 +1,20 @@
|
||||
package me.bearns.fias.service;
|
||||
|
||||
import me.bearns.fias.domain.FiasVersion;
|
||||
import me.bearns.fias.exceptions.CommonException;
|
||||
import me.bearns.fias.exceptions.DownloadException;
|
||||
import me.bearns.fias.exceptions.UnzipException;
|
||||
import me.bearns.fias.repository.FiasVersionRepository;
|
||||
import me.bearns.fias.util.ZipReader;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipException;
|
||||
import java.util.zip.ZipFile;
|
||||
|
||||
@Service
|
||||
public class FiasUpdater implements Updater {
|
||||
@@ -29,7 +29,7 @@ public class FiasUpdater implements Updater {
|
||||
OnlineVersion clientStub;
|
||||
|
||||
@Override
|
||||
public void update() throws DownloadException, UnzipException {
|
||||
public void update() throws CommonException {
|
||||
|
||||
//Max value
|
||||
FiasVersion dbVersionObj = versions.findTopByOrderByVersionIdDesc();
|
||||
@@ -55,7 +55,7 @@ public class FiasUpdater implements Updater {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void reload(Long... regions) throws DownloadException, UnzipException {
|
||||
public void reload(Long... regions) throws CommonException {
|
||||
|
||||
final List<FiasVersion> lastVersion = clientStub.getLastVersion();
|
||||
if(lastVersion != null) {
|
||||
@@ -65,7 +65,7 @@ public class FiasUpdater implements Updater {
|
||||
}
|
||||
|
||||
//transaction from here
|
||||
private void processUpdates(List<FiasVersion> updates, boolean reloadFlag, Long ... regions) throws DownloadException, UnzipException {
|
||||
private void processUpdates(List<FiasVersion> updates, boolean reloadFlag, Long ... regions) throws CommonException {
|
||||
|
||||
if(reloadFlag) {
|
||||
//todo reload flag impl
|
||||
@@ -100,34 +100,37 @@ public class FiasUpdater implements Updater {
|
||||
e.printStackTrace();
|
||||
//todo log
|
||||
throw new DownloadException(e);
|
||||
} catch (FileNotFoundException e) {
|
||||
throw new UnzipException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void processArchive(File file, Long ... regions) throws FileNotFoundException, UnzipException {
|
||||
private void processArchive(File file, Long ... regions) throws CommonException {
|
||||
|
||||
try(final ZipReader reader = new ZipReader(new FileInputStream(file))){
|
||||
try(final ZipFile zipFile = new ZipFile(file)){
|
||||
|
||||
final Set<String> list = reader.list();
|
||||
final Enumeration<? extends ZipEntry> entries = zipFile.entries();
|
||||
|
||||
list.forEach(s -> {
|
||||
try(final InputStream inputStream = reader.read(s)) {
|
||||
while (entries.hasMoreElements()){
|
||||
|
||||
//todo
|
||||
final ZipEntry entry = entries.nextElement();
|
||||
|
||||
if(check(entry)) processEntry(zipFile.getInputStream(entry), regions);
|
||||
}
|
||||
|
||||
} catch (UnzipException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
});
|
||||
|
||||
} catch (ZipException e) {
|
||||
throw new UnzipException(e);
|
||||
} catch (IOException e) {
|
||||
throw new UnzipException(e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void processEntry(InputStream is, Long ... regions){
|
||||
//todo
|
||||
}
|
||||
|
||||
private boolean check(ZipEntry entry){
|
||||
//todo
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package me.bearns.fias.service;
|
||||
|
||||
import me.bearns.fias.exceptions.CommonException;
|
||||
import me.bearns.fias.exceptions.DownloadException;
|
||||
import me.bearns.fias.exceptions.UnzipException;
|
||||
|
||||
@@ -9,11 +10,11 @@ public interface Updater {
|
||||
/**
|
||||
* Run online update for fias database
|
||||
*/
|
||||
public void update() throws DownloadException, UnzipException;
|
||||
public void update() throws CommonException;
|
||||
|
||||
/**
|
||||
* Load or reload database for selected regions
|
||||
* @param regions
|
||||
*/
|
||||
public void reload(Long ... regions) throws DownloadException, UnzipException;
|
||||
public void reload(Long ... regions) throws CommonException;
|
||||
}
|
||||
|
||||
@@ -1,164 +0,0 @@
|
||||
package me.bearns.fias.util;
|
||||
|
||||
import me.bearns.fias.exceptions.UnzipException;
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipInputStream;
|
||||
|
||||
public class ZipReader implements Closeable {
|
||||
|
||||
private final InputStream in;
|
||||
private final Set<String> set = new HashSet<>();
|
||||
private final ReentrantLock mutex = new ReentrantLock();
|
||||
|
||||
public ZipReader(InputStream in){
|
||||
this.in = in;
|
||||
}
|
||||
|
||||
public Set<String> list() throws UnzipException {
|
||||
|
||||
if(set.isEmpty()) init();
|
||||
|
||||
return set;
|
||||
|
||||
}
|
||||
|
||||
public InputStream read(String file) throws UnzipException {
|
||||
|
||||
if(file == null) throw new UnzipException("Filename in archive cannot be null");
|
||||
|
||||
mutex.lock();
|
||||
|
||||
InputStream retval = null;
|
||||
ZipInputStream zis = null;
|
||||
|
||||
try{
|
||||
zis = new ZipInputStream(in);
|
||||
|
||||
ZipEntry zipEntry;
|
||||
while ((zipEntry = zis.getNextEntry()) != null) {
|
||||
|
||||
if(file.equals(zipEntry.getName())) {
|
||||
//TODO
|
||||
retval = new FileStream(zis);
|
||||
|
||||
}
|
||||
zis.closeEntry();
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
throw new UnzipException(e);
|
||||
} finally {
|
||||
if(retval == null) {
|
||||
|
||||
mutex.unlock();
|
||||
|
||||
try {
|
||||
if(zis!=null) zis.close();
|
||||
} catch (IOException e) {
|
||||
//log
|
||||
}
|
||||
|
||||
throw new UnzipException("File not found in archive");
|
||||
}
|
||||
}
|
||||
|
||||
return retval;
|
||||
|
||||
}
|
||||
|
||||
private synchronized void init() throws UnzipException {
|
||||
|
||||
mutex.lock();
|
||||
|
||||
try(ZipInputStream zis = new ZipInputStream(in)){
|
||||
|
||||
ZipEntry zipEntry;
|
||||
while ((zipEntry = zis.getNextEntry()) != null) {
|
||||
|
||||
set.add(zipEntry.getName());
|
||||
zis.closeEntry();
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
throw new UnzipException(e);
|
||||
} finally {
|
||||
mutex.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
|
||||
mutex.unlock();
|
||||
|
||||
if(in!=null) {
|
||||
in.close();
|
||||
}
|
||||
}
|
||||
|
||||
public class FileStream extends InputStream{
|
||||
|
||||
private ZipInputStream zis;
|
||||
|
||||
public FileStream(ZipInputStream zis) {
|
||||
this.zis = zis;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int read() throws IOException {
|
||||
return zis.read();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int read(byte[] b) throws IOException {
|
||||
return zis.read(b);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int read(byte[] b, int off, int len) throws IOException {
|
||||
return zis.read(b, off, len);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long skip(long n) throws IOException {
|
||||
return zis.skip(n);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int available() throws IOException {
|
||||
return zis.available();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
try {
|
||||
zis.closeEntry();
|
||||
zis.close();
|
||||
} finally {
|
||||
mutex.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized void mark(int readlimit) {
|
||||
zis.mark(readlimit);
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized void reset() throws IOException {
|
||||
zis.reset();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean markSupported() {
|
||||
return zis.markSupported();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user