2

Is there a way to put a directory full of zip files inside a list in a java springboot application? Say for example in my local:

/Macintosh HD/Systems/Volumes/Macintosh HD/folder1/profile/sent

There is a bunch of zip files and trg files, and I want to load these into eclipse java and look at the contents inside of each one (this is where the list comes in from I assume), how would I do that?

I tried doing

String folder1path = "/Macintosh HD/Systems/Volumes/Macintosh HD/folder1/profile/sent";

List<File> fileList = Arrays.asList(new File(folder1path).listFiles());

logger.info(fileList);

But this results in an list that looks like:

[
/Macintosh HD/Systems/Volumes/Macintosh HD/folder1/profile/sent/testZip1.zip
/Macintosh HD/Systems/Volumes/Macintosh HD/folder1/profile/sent/testZip2.trg
/Macintosh HD/Systems/Volumes/Macintosh HD/folder1/profile/sent/testZip5.zip
/Macintosh HD/Systems/Volumes/Macintosh HD/folder1/profile/sent/testZip3.zip
/Macintosh HD/Systems/Volumes/Macintosh HD/folder1/profile/sent/testZip4.zip
/Macintosh HD/Systems/Volumes/Macintosh HD/folder1/profile/sent/testZip1.trg
/Macintosh HD/Systems/Volumes/Macintosh HD/folder1/profile/sent/testZip2.zip
/Macintosh HD/Systems/Volumes/Macintosh HD/folder1/profile/sent/testZip4.trg
...
]

I haven't worked with this kind of stuff before but this isn't a "Zip" type file I think? It's a type File, but it has the path string in front. I don't think I can even open these zips either, so I was wondering if there was like a Zip type in java, and then I could access the contents inside the zip (it would be mostly excel spreadsheets inside, but I don't need to access the excel spreadsheets)?

stackerstack
  • 243
  • 4
  • 16

1 Answers1

2

What you have so far is good? You have all the zip file names.

If you want to access the contents of the zip files (unzipping), then there are a few options. The JDK has java.util.zip.ZipFile OR you can try existing tools such as zip4j:

new ZipFile("filename.zip").extractAll("/destination_directory");

Possible helpful stackoverflow:

What is a good Java library to zip/unzip files?

========= Edited:

Get FileAttributes, then use the lastModifiedTime method:

try {

    BasicFileAttributes fileAttributes = Files.readAttributes(new File("C:\\some_file.zip").toPath(),BasicFileAttributes.class);
    
    System.out.println(fileAttributes.lastModifiedTime());

} catch (IOException e) {
    e.printStackTrace();
}

There are other ways of listing a directory:

List all files end with .java

try (Stream<Path> walk = Files.walk(Paths.get("C:\\projects"))) {

    List<String> result = walk.map(x -> x.toString())
            .filter(f -> f.endsWith(".java")).collect(Collectors.toList());

    result.forEach(System.out::println);

} catch (IOException e) {
    e.printStackTrace();
}

https://mkyong.com/java/java-how-to-list-all-files-in-a-directory/

JCompetence
  • 6,997
  • 3
  • 19
  • 26
  • i found it weird that i was using the "File" type though, because does a .zip count as a File type in java? It has that path attached to the front, but I was thinking it would directly just have the zip file name. Like instead of specifying an existing zip file name in "new ZipFile(...)", is there a way for java to already know that its a ZipFile type? – stackerstack Dec 02 '20 at 22:44
  • a ZipFile is a File. When you list a directory, it returns the all the (Files), with their extensions. It is up to you how to handle them? . If a file ends with .jpg (It is still a file), but you would need a program to view it as an Image. Same with a .zip (It is a file), you just need a way to unzip it. Java provides that option if needed, plus other libs. It doesnt load up a list of [ZipFile,ZipFile,ZipFile] by default. You can do that yourself i you want. – JCompetence Dec 03 '20 at 07:45
  • https://stackoverflow.com/questions/15667125/read-content-from-files-which-are-inside-zip-file – JCompetence Dec 03 '20 at 07:46
  • is there a way to grab the date modified of the ZIP file itself, and not the actual files inside the zip file? – stackerstack Dec 03 '20 at 15:17
  • @stackerstack Yes I updated the answer to show you how to get the lastModifiedTime of the zipfile (NOT its contents) – JCompetence Dec 03 '20 at 15:35