Skip to content

Commit 93e7197

Browse files
authored
Merge pull request IQSS#10627 from IQSS/8945-prevent-mislabelling-non-shapefiles-in-zip
ignore shapefiles if they are under a hidden directory in the zip file
2 parents 5ba74e8 + 1b3e312 commit 93e7197

File tree

4 files changed

+38
-17
lines changed

4 files changed

+38
-17
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
### Shapefile Handling will now ignore files under a hidden directory within the zip file
2+
3+
Directories that are hidden will be ignored when determining if a zip file contains Shapefile files.
4+
5+
For more information, see #8945.

src/main/java/edu/harvard/iq/dataverse/util/ShapefileHandler.java

+26-16
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import java.util.*;
1616

1717
import java.nio.file.Files;
18+
import java.nio.file.Paths;
1819
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
1920
import java.util.logging.Level;
2021
import java.util.logging.Logger;
@@ -695,33 +696,42 @@ private boolean examineZipfile(FileInputStream zip_file_stream){
695696
this.filesListInDir.clear();
696697
this.filesizeHash.clear();
697698
this.fileGroups.clear();
698-
699-
try{
699+
700+
try{
700701
ZipInputStream zipStream = new ZipInputStream(zip_file_stream);
701702
ZipEntry entry;
702-
703+
List<String> hiddenDirectories = new ArrayList<>();
703704
while((entry = zipStream.getNextEntry())!=null){
705+
String zentryFileName = entry.getName();
706+
boolean isDirectory = entry.isDirectory();
707+
708+
Boolean skip = isDirectory || this.isFileToSkip(zentryFileName);
709+
710+
// check if path is hidden
711+
if (isDirectory && Files.isHidden(Paths.get(zentryFileName))) {
712+
hiddenDirectories.add(zentryFileName);
713+
logger.fine("Ignoring files under hidden directory: " + zentryFileName);
714+
} else {
715+
// check if the path was already found to be hidden
716+
for (String hidden : hiddenDirectories) {
717+
if (zentryFileName.startsWith(hidden)) {
718+
skip = true;
719+
break;
720+
}
721+
}
722+
}
704723

705-
String zentryFileName = entry.getName();
706-
//msg("zip entry: " + entry.getName());
707-
// Skip files or folders starting with __
708-
if (this.isFileToSkip(zentryFileName)){
709-
continue;
710-
}
711-
712-
if (entry.isDirectory()) {
713-
//String dirpath = outputFolder + "/" + zentryFileName;
714-
//createDirectory(dirpath);
715-
continue;
724+
if (skip) {
725+
continue;
716726
}
717-
727+
718728
String unzipFileName = this.getFileBasename(zentryFileName);
719729
if (unzipFileName==null){
720730
logger.warning("Zip Entry Basename is an empty string: " + zentryFileName);
721731
continue;
722732
}
723733
String unzipFolderName = this.getFolderName(zentryFileName);
724-
734+
725735
String unzipFilePath = unzipFileName;
726736
if (unzipFolderName != null) {
727737
unzipFilePath = unzipFolderName + "/" + unzipFileName;

src/test/java/edu/harvard/iq/dataverse/util/shapefile/ShapefileHandlerTest.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -282,8 +282,14 @@ public void testZippedShapefileWithExtraFiles() throws IOException{
282282

283283
msg("Passed!");
284284
}
285-
286285

286+
@Test
287+
public void testHiddenFiles() {
288+
// test with shapefiles in hidden directory
289+
ShapefileHandler shp_handler = new ShapefileHandler("src/test/resources/hiddenShapefiles.zip");
290+
shp_handler.DEBUG= true;
291+
assertFalse(shp_handler.containsShapefile());
292+
}
287293

288294

289295

53.2 KB
Binary file not shown.

0 commit comments

Comments
 (0)