75 lines
1.8 KiB
Java
75 lines
1.8 KiB
Java
package eu.dnetlib.apps.oai.storage;
|
|
|
|
import java.io.File;
|
|
import java.io.FileOutputStream;
|
|
import java.io.IOException;
|
|
import java.util.zip.ZipEntry;
|
|
import java.util.zip.ZipOutputStream;
|
|
|
|
import org.apache.commons.io.FileUtils;
|
|
import org.apache.commons.logging.Log;
|
|
import org.apache.commons.logging.LogFactory;
|
|
|
|
public class ZipStorage implements StorageClient {
|
|
|
|
private static final Log log = LogFactory.getLog(ZipStorage.class);
|
|
|
|
private FileOutputStream fos;
|
|
private ZipOutputStream zipOut;
|
|
private long currPage = -1;
|
|
|
|
@Override
|
|
public void login(final String user, final String password) {}
|
|
|
|
@Override
|
|
public void complete() {
|
|
try {
|
|
if (zipOut != null) {
|
|
zipOut.close();
|
|
}
|
|
if (fos != null) {
|
|
fos.close();
|
|
}
|
|
} catch (final IOException e) {
|
|
log.error("Ftp Disconnection Failed");
|
|
throw new RuntimeException(e);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void init(final String baseDir, final String jobId) {
|
|
try {
|
|
final File rootDir = new File(baseDir);
|
|
FileUtils.forceMkdir(rootDir);
|
|
|
|
this.fos = new FileOutputStream(rootDir.getAbsolutePath() + "/" + jobId + ".zip");
|
|
this.zipOut = new ZipOutputStream(fos);
|
|
} catch (final IOException e) {
|
|
throw new RuntimeException("Error preparing zip", e);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void prepareCurrentPage(final long page) {
|
|
try {
|
|
zipOut.putNextEntry(new ZipEntry(page + "/"));
|
|
zipOut.closeEntry();
|
|
this.currPage = page;
|
|
} catch (final IOException e) {
|
|
throw new RuntimeException("Error adding a directory to zip", e);
|
|
}
|
|
|
|
}
|
|
|
|
@Override
|
|
public void saveFile(final String filename, final String body) {
|
|
try {
|
|
zipOut.putNextEntry(new ZipEntry(new ZipEntry(this.currPage + "/" + filename)));
|
|
zipOut.write(body.getBytes());
|
|
} catch (final IOException e) {
|
|
throw new RuntimeException("Error adding a file to zip", e);
|
|
}
|
|
}
|
|
|
|
}
|