CodeGym /Courses /Java Multithreading /Big task: Writing an archiver in Java

Big task: Writing an archiver in Java

Java Multithreading
Level 7 , Lesson 15
Available

"Hi, Amigo!"

"Hello, Captain Squirrels, sir!"

"Ready for a new secret mission?"

"Of course, I'm ready, sir!"

"Then here's a file with instructions. Today we're going to develop a new kind of artificial intelligence. The human race needs our help. We must save people from destruction."

"But, sir! I can't open the file. I need an archiver."

"Really? Hmm... Then the salvation of mankind is postponed. Today, we'll write our own archiver."

Big task: Writing an archiver in Java - 1

"Captain, what about the people?"

"Nothing will happen to them. Contact Agent IntelliJ IDEA for your assignment. He'll give you all the instructions."

"May I proceed, sir?"

"Proceed."

16
Task
Java Multithreading, level 7, lesson 15
Locked
Archiver (part 1)
Let's write an archiver. At a minimum, an archiver should be able to zip and unzip files. Let's start with the first one. We need an archive manager. It will perform operations on the archive file (a file that will be stored on disk and have the zip extension).
32
Task
Java Multithreading, level 7, lesson 15
Locked
Archiver (part 2)
Now let's implement the createZip(Path source) method, which will zip the file specified by the source argument. Java has a special ZipOutputStream class in the java.util.zip package. It compresses (zips) the data passed to it.
16
Task
Java Multithreading, level 7, lesson 15
Locked
Archiver (part 3)
As you can see, zipping isn't that bad. But our archiver is somehow too primitive. A real archiver should be able to do much more: extract an archive, add a new file to an existing archive, remove a file from an archive, and view the contents of an archive. Now let's improve our archiver.
16
Task
Java Multithreading, level 7, lesson 15
Locked
Archiver (part 4)
Each command implies the execution of some action. Create a Command interface with an execute() method. We'll create a separate class for each command. All of the command classes must implement (inherit) the Command interface.
8
Task
Java Multithreading, level 7, lesson 15
Locked
Archiver (part 5)
Let's divide the commands into two types: those that work directly with the archive, and helpers (for example, EXIT). All the commands of the first type will have shared functionality. It's convenient to pull this functionality into a common base class. Let's call this class ZipCommand.
16
Task
Java Multithreading, level 7, lesson 15
Locked
Archiver (part 6)
Suppose the user uses the Operation operation variable to let us know what he or she wants to do. Then we need to check the variable, create a corresponding command object, and call its execute() method. To avoid creating the required command object every time, we need to store it somewhere.
8
Task
Java Multithreading, level 7, lesson 15
Locked
Archiver (part 7)
We've done a lot and can take a quick break. Let's create an exception package and add two classes to it: PathNotFoundException and NoSuchZipFileException. We'll throw a PathNotFoundException exception if we can't find the path needed to extract the archive, or the path to the file you want to zip.
32
Task
Java Multithreading, level 7, lesson 15
Locked
Archiver (part 8)
To find out which command the user currently wants to execute, let's add an Operation askOperation() method to the Archiver class. This method should display a list of available commands and ask the user to choose one of them.
32
Task
Java Multithreading, level 7, lesson 15
Locked
Archiver (part 9)
Now let's take care of another part of our archiver that is no less important. Frequently, users want to create an archive from not just one file, but from an entire folder. In this case, zipping amounts to successively adding a ZipEntry for each file to the archive.
32
Task
Java Multithreading, level 7, lesson 15
Locked
Archiver (part 10)
It's time to refactor the ZipFileManager class. The createZip method has code that we're also going to need in the methods that will add files to or remove them from an archive, extract files, etc. We'll implement those methods later, but we can move the common code to separate methods now.
32
Task
Java Multithreading, level 7, lesson 15
Locked
Archiver (part 11)
Finally, we'll try to provide a decent implementation for the ZipCreateCommand class's execute() method that we added previously. To do this, we need to: 1. Display "Creating an archive." at the beginning of the method. Don't forget that we are using the ConsoleHelper class to work with the console.
16
Task
Java Multithreading, level 7, lesson 15
Locked
Archiver (part 12)
Today we'll prepare to implement ZipContentCommand. It will be responsible for getting an archive's contents. The archive's contents are compressed files and folders, but we're interested in knowing not only the names of the archive's objects, but also their size before and after compression.
32
Task
Java Multithreading, level 7, lesson 15
Locked
Archiver (part 13)
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).
8
Task
Java Multithreading, level 7, lesson 15
Locked
Archiver (part 14)
Everything is ready to implement the ZipContentCommand class's execute() method: 1. Display "Viewing contents of the archive."; 2. Create a ZipFileManager object using the getZipFileManager() method; 3. Display "Archive contents:"; 4. Get a list of the archive's files using the getFileList() method.
32
Task
Java Multithreading, level 7, lesson 15
Locked
Archiver (part 15)
It's time to try to extract something. To do this, add a public void extractAll(Path outputFolder) throws Exception method to the ZipFileManager class. The Path outputFolder parameter is the path to which we will extract our archive.
32
Task
Java Multithreading, level 7, lesson 15
Locked
Archiver (part 16)
It's time to remove something from an archive. An archive is a tricky thing—you can't just go and remove an element. Why? Imagine we decided to invent our own text compression algorithm. After examining the source text, we see that the phrase "being a programmer is cool" occurs often.
32
Task
Java Multithreading, level 7, lesson 15
Locked
Archiver (part 17)
There's just one small trifle left: adding a file to an archive. It sounds shady, but that's exactly what we're going to do. Adding files is similar to removing them: we create a temporary archive file, copy over all of the old archive's contents, and then add the new files.
8
Task
Java Multithreading, level 7, lesson 15
Locked
Archiver (part 18)
You're an excellent student! I've slightly tweaked your code. You can use this archiver for zipping files in everyday life. If you have the time and desire, try adding an operation that varies the zip compression level!
Comments (13)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Jaime Padilla Level 41, Chandler, United States Expert
3 November 2023
From Quora: "In few words, 1 kilobyte contains 1000 bytes. Divide bytes by 1000 and you'll get kilobytes. Back in the day when computers weren't that powerful, 1 kilobyte was exactly 1024 bytes, but it seems people didn't like the number and decided that 1 kilobyte was equal to 1000 bytes to make things easier to remember. As time went by, the idea that 1 kb=1000 bytes grew in popularity and most people agree with it; on the other hand, there are still many who consider that 1 kb is 1024 bytes. In 1998, in order to stop the confusion, the International Electrotechnical Commission (IEC) and the National Institute of Standards and Time (NIST) created the unit “kibibyte (KiB).” Since that moment, a kibibyte is 1024 bytes and the older unit, the kilobyte (kB), equals to 1000 bytes. Even though this should have killed the confusion, it only made it worse." I am still confused, why did part 12 use 1024? If 1 byte is considered 1000kb in current times?
matemate123 Level 50, Kraków, Poland
27 June 2023
I think it could be useful, when after each part that code is updated by codegym, we could get our own code back and compare with clean code from mentor. Great Big Task, I like it a lot. Useable program.
Максим Василенко Level 44, Kiev, Ukraine
14 February 2023
How is this task related to multithreading?
Justin Smith Level 41, Greenfield, USA, United States
27 April 2022
This one is pretty difficult in places. But a lot of interesting things to learn about working with zip files. I am surprised, though, that Java does not simply provide built-in code to zip and unzip files. Also, this project was kind of weird in that it has nothing to do with multithreading at all.
Andrei Level 41
12 May 2021
Has anybody really understood the whole program? Or at one point, you started going with the flow of the requests because you lost track of what each method does and how everything connects...
Thành Black Level 49, Hanoi
21 November 2021
How are you now, I have many difficulties here >>
Shilpa nori Level 34, Rochester, United States
1 April 2021
Almost all the tasks are tough. Spent a lot of time and finished these, not sure i remember anything now . Feels great to move on
Denys Level 41, Ukraine
27 February 2021
17 level: pass arguments like that Files.createTempFile(null, null);
Henrique Level 41, São Paulo, Brazil
12 August 2020
OMG why does it have to be so confusing? There's no help, so those who don't understand will just be stuck in some part forever, until someone good-hearted appears.
Vesa Level 41, Kaliningrad, Russia
2 June 2020
In part 8, before rewriting the main () method, you must remove the old code from this method.
Niladri pradhan Level 33, Mumbai, India
5 December 2019
I am not able to pass last condition of task 8.Could someone please help.
Juan Ma Level 41, Arauca, Colombia
20 April 2020
It may be late but try do{...}while() instead of while(){...}.