CodeGym
Promotion
CodeGym University
Learning
Course
Tasks
Surveys & Quizzes
Games
Help
Schedule
Community
Users
Forum
Chat
Articles
Success stories
Activity
Reviews
Subscriptions
Light theme
Start learning now
  • All questions
Youngsan
Level 41
Seoul
  • 07.10.2020
  • 282views
  • 3comments

Pls help.

Question about the task Archiver (part 13)
Java Multithreading,  Level 7,  Lesson 15
Under discussion

Let's continue to move toward getting the contents of an archive. Let's write a getFileList() method inside the ZipFileManager class. It will return a list of the files in the archive, or rather a list of the properties of these files (we already implemented the FileProperties class).
Let's begin:
1. Add a List<FileProperties> getFileList() throws Exception method to the ZipFileManager class.
2. Inside that method, check whether zipFile is a regular file using a suitable method from the Files class. If it is not a file, throw a NoSuchZipFileException.
3. Create a list of FileProperties objects. We'll add the file properties to it.
4. Create a ZipInputStream for the file represented by the zipFile variable. As we've done before, wrap the creation of this object in a try-with-resources.
5. Go through all of the ZipEntries in the ZipInputStream
6. For each ZipEntry, read its contents (otherwise, we won't have information about its size). You can't find the size of a file in an archive without reading it. This is super easy to do using the copyData method and a temporary ByteArrayOutputStream buffer.
7. Get the name, size, compressed size, and compression method of the zip entry. Take a look at what else you can learn about the entry.
8. Create a FileProperties object using the obtained file data.
9. Add the object created in step 8 to the list from step 3.
10. Return the gathered information to the method that called the getFileList method.

Requirements:
  • You need to create a public List<FileProperties> getFileList() throws Exception method in the ZipFileManager class.
  • If Path zipFile is not a file, then the getFileList method should throw a NoSuchZipFileException.
  • The getFileList method should go through all of the archive's files and return data about them. The method must be implemented according to the task conditions.
  • The stream used to read from the archive must be closed.
package com.codegym.task.task31.task3110; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; import java.nio.file.Paths; import com.codegym.task.task31.task3110.command.Command; import com.codegym.task.task31.task3110.exception.PathNotFoundException; import com.codegym.task.task31.task3110.exception.NoSuchZipFileException; public class Archiver { public static void main(String[] args) throws IOException { Operation operation = null; do { try { operation = askOperation(); CommandExecutor.execute(operation); } catch (NoSuchZipFileException e) { ConsoleHelper.writeMessage("You didn't select an archive or you selected an invalid file."); } catch (Exception e) { ConsoleHelper.writeMessage("An error occurred. Please check the entered data."); } } while (operation != Operation.EXIT); } public static Operation askOperation() throws IOException { ConsoleHelper.writeMessage(""); ConsoleHelper.writeMessage("Select an operation:"); ConsoleHelper.writeMessage(String.format("\t %d - Zip files into an archive", Operation.CREATE.ordinal())); ConsoleHelper.writeMessage(String.format("\t %d - Add a file to an archive", Operation.ADD.ordinal())); ConsoleHelper.writeMessage(String.format("\t %d - Remove a file from an archive", Operation.REMOVE.ordinal())); ConsoleHelper.writeMessage(String.format("\t %d - Extract an archive", Operation.EXTRACT.ordinal())); ConsoleHelper.writeMessage(String.format("\t %d - View the contents of an archive", Operation.CONTENT.ordinal())); ConsoleHelper.writeMessage(String.format("\t %d - Exit", Operation.EXIT.ordinal())); return Operation.values()[ConsoleHelper.readInt()]; } }
0
Comments (3)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Guadalupe Gagnon
Level 37 , Tampa, United States
7 October 2020, 15:26
#1 lines 63-64 should be uncommented #2 line 68 should be inside the while loop, right before it ends a cycle.
+2
Youngsan
Level 41 , Seoul, Korea, Republic of
7 October 2020, 17:28
I tried, but still failed to be verified. #1 lines 63-64 should be uncommented. - No difference. #2 line 68 should be inside the while loop, right before it ends a cycle. - Tried in while loop, but failed. I don't understand, but something is wrong. I found exactly same sources through web surfing, seemed to be verified. Thanks.
0
Banak
Level 29 , Saint-Gratien, France
21 November 2021, 20:35
the try-with-resources closes the zipInputStream. I think you don't need to close it at the end.
0
Learn
  • Registration
  • Java Course
  • Help with Tasks
  • Pricing
  • Game Projects
  • Java Syntax
Community
  • Users
  • Articles
  • Forum
  • Chat
  • Success Stories
  • Activity
  • Affiliate Program
Company
  • About us
  • Contacts
  • Reviews
  • Press Room
  • CodeGym for EDU
  • FAQ
  • Support
CodeGym CodeGym is an online course for learning Java programming from scratch. This course is a perfect way to master Java for beginners. It contains 1200+ tasks with instant verification and an essential scope of Java fundamentals theory. To help you succeed in education, we’ve implemented a set of motivational features: quizzes, coding projects, content about efficient learning and Java developer’s career.
Follow us
Interface language
Programmers Are Made, Not Born © 2023 CodeGym
MastercardVisa
Programmers Are Made, Not Born © 2023 CodeGym
This website uses cookies to provide you with personalized service. By using this website, you agree to our use of cookies. If you require more details, please read our Terms and Policy.