Skip to content

Commit

Permalink
Merge pull request #10899 from DANS-KNAW-jp/10898-own-cloud-zips
Browse files Browse the repository at this point in the history
replace ZipInputStream with ZipFile
  • Loading branch information
ofahimIQSS authored Oct 25, 2024
2 parents 9faf32e + aeb6d2a commit d09e509
Show file tree
Hide file tree
Showing 8 changed files with 507 additions and 360 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -100,71 +100,48 @@ public IngestServiceShapefileHelper(File zippedShapefile, File rezipFolder){
//this.processFile(zippedShapefile, rezipFolder);

}
private FileInputStream getFileInputStream(File fileObject){
if (fileObject==null){
return null;
}
try {

private FileInputStream getFileInputStream(File fileObject){
if (fileObject==null){
return null;
}
try {
return new FileInputStream(fileObject);
} catch (FileNotFoundException ex) {
logger.severe("Failed to create FileInputStream from File: " + fileObject.getAbsolutePath());
return null;
}
}
private void closeFileInputStream(FileInputStream fis){
if (fis==null){
return;
}
}

private void closeFileInputStream(FileInputStream fis){
if (fis==null){
return;
}
try {
fis.close();
fis.close();
} catch (IOException ex) {
logger.info("Failed to close FileInputStream");
}
}
}

public boolean processFile() {

if ((!isValidFile(this.zippedShapefile))||(!isValidFolder(this.rezipFolder))){
return false;
}

// (1) Use the ShapefileHandler to the .zip for a shapefile
//
FileInputStream shpfileInputStream = this.getFileInputStream(zippedShapefile);
if (shpfileInputStream==null){
return false;
}

this.shpHandler = new ShapefileHandler(shpfileInputStream);
if (!shpHandler.containsShapefile()){
logger.severe("Shapefile was incorrectly detected upon Ingest (FileUtil) and passed here");
return false;
}

this.closeFileInputStream(shpfileInputStream);

// (2) Rezip the shapefile pieces
logger.info("rezipFolder: " + rezipFolder.getAbsolutePath());
shpfileInputStream = this.getFileInputStream(zippedShapefile);
if (shpfileInputStream==null){
return false;
}

boolean rezipSuccess;
try {
rezipSuccess = shpHandler.rezipShapefileSets(shpfileInputStream, rezipFolder);
this.shpHandler = new ShapefileHandler(zippedShapefile);
if (!shpHandler.containsShapefile()){
logger.severe("Shapefile was incorrectly detected upon Ingest (FileUtil) and passed here");
return false;
}
logger.info("rezipFolder: " + rezipFolder.getAbsolutePath());
return shpHandler.rezipShapefileSets(rezipFolder);
} catch (IOException ex) {
logger.severe("Shapefile was not correctly unpacked/repacked");
logger.severe("shpHandler message: " + shpHandler.errorMessage);
return false;
}

this.closeFileInputStream(shpfileInputStream);

return rezipSuccess;

// return createDataFiles(rezipFolder);

}
Expand Down
13 changes: 8 additions & 5 deletions src/main/java/edu/harvard/iq/dataverse/util/FileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -525,15 +525,18 @@ public static String determineFileType(File f, String fileName) throws IOExcepti
// Check for shapefile extensions as described here: http://en.wikipedia.org/wiki/Shapefile
//logger.info("Checking for shapefile");

ShapefileHandler shp_handler = new ShapefileHandler(new FileInputStream(f));
ShapefileHandler shp_handler = new ShapefileHandler(f);
if (shp_handler.containsShapefile()){
// logger.info("------- shapefile FOUND ----------");
fileType = ShapefileHandler.SHAPEFILE_FILE_TYPE; //"application/zipped-shapefile";
}

Optional<BagItFileHandler> bagItFileHandler = CDI.current().select(BagItFileHandlerFactory.class).get().getBagItFileHandler();
if(bagItFileHandler.isPresent() && bagItFileHandler.get().isBagItPackage(fileName, f)) {
fileType = BagItFileHandler.FILE_TYPE;
try {
Optional<BagItFileHandler> bagItFileHandler = CDI.current().select(BagItFileHandlerFactory.class).get().getBagItFileHandler();
if (bagItFileHandler.isPresent() && bagItFileHandler.get().isBagItPackage(fileName, f)) {
fileType = BagItFileHandler.FILE_TYPE;
}
} catch (Exception e) {
logger.warning("Error checking for BagIt package: " + e.getMessage());
}
}

Expand Down
Loading

0 comments on commit d09e509

Please sign in to comment.