Skip to content

Commit

Permalink
Merge pull request #10627 from IQSS/8945-prevent-mislabelling-non-sha…
Browse files Browse the repository at this point in the history
…pefiles-in-zip

ignore shapefiles if they are under a hidden directory in the zip file
  • Loading branch information
sekmiller authored Jul 16, 2024
2 parents 5ba74e8 + 1b3e312 commit 93e7197
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
### Shapefile Handling will now ignore files under a hidden directory within the zip file

Directories that are hidden will be ignored when determining if a zip file contains Shapefile files.

For more information, see #8945.
42 changes: 26 additions & 16 deletions src/main/java/edu/harvard/iq/dataverse/util/ShapefileHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.*;

import java.nio.file.Files;
import java.nio.file.Paths;
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand Down Expand Up @@ -695,33 +696,42 @@ private boolean examineZipfile(FileInputStream zip_file_stream){
this.filesListInDir.clear();
this.filesizeHash.clear();
this.fileGroups.clear();
try{

try{
ZipInputStream zipStream = new ZipInputStream(zip_file_stream);
ZipEntry entry;

List<String> hiddenDirectories = new ArrayList<>();
while((entry = zipStream.getNextEntry())!=null){
String zentryFileName = entry.getName();
boolean isDirectory = entry.isDirectory();

Boolean skip = isDirectory || this.isFileToSkip(zentryFileName);

// check if path is hidden
if (isDirectory && Files.isHidden(Paths.get(zentryFileName))) {
hiddenDirectories.add(zentryFileName);
logger.fine("Ignoring files under hidden directory: " + zentryFileName);
} else {
// check if the path was already found to be hidden
for (String hidden : hiddenDirectories) {
if (zentryFileName.startsWith(hidden)) {
skip = true;
break;
}
}
}

String zentryFileName = entry.getName();
//msg("zip entry: " + entry.getName());
// Skip files or folders starting with __
if (this.isFileToSkip(zentryFileName)){
continue;
}

if (entry.isDirectory()) {
//String dirpath = outputFolder + "/" + zentryFileName;
//createDirectory(dirpath);
continue;
if (skip) {
continue;
}

String unzipFileName = this.getFileBasename(zentryFileName);
if (unzipFileName==null){
logger.warning("Zip Entry Basename is an empty string: " + zentryFileName);
continue;
}
String unzipFolderName = this.getFolderName(zentryFileName);

String unzipFilePath = unzipFileName;
if (unzipFolderName != null) {
unzipFilePath = unzipFolderName + "/" + unzipFileName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,8 +282,14 @@ public void testZippedShapefileWithExtraFiles() throws IOException{

msg("Passed!");
}


@Test
public void testHiddenFiles() {
// test with shapefiles in hidden directory
ShapefileHandler shp_handler = new ShapefileHandler("src/test/resources/hiddenShapefiles.zip");
shp_handler.DEBUG= true;
assertFalse(shp_handler.containsShapefile());
}



Expand Down
Binary file added src/test/resources/hiddenShapefiles.zip
Binary file not shown.

0 comments on commit 93e7197

Please sign in to comment.