“嗨,阿米戈!”
“您好,松鼠船長,先生!”
“準備好接受新的秘密任務了嗎?”
“當然,我準備好了,長官!”
“那就來一份文件,裡面有說明,今天我們要研發一種新型的人工智能,人類需要我們的幫助,我們必須拯救人類免於毀滅。”
“但是,先生!我無法打開文件。我需要一個存檔器。”
“是嗎?嗯……那麼人類的救贖就延期了,今天,我們自己寫存檔。”

“隊長,那些人呢?”
“他們不會有事的。聯繫 IntelliJ IDEA 特工來完成你的任務。他會給你所有的指示。”
“我可以繼續嗎,先生?”
“繼續。”
16
任務
Java Multithreading, 等級 7, 課堂 15
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
任務
Java Multithreading, 等級 7, 課堂 15
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
任務
Java Multithreading, 等級 7, 課堂 15
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
任務
Java Multithreading, 等級 7, 課堂 15
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
任務
Java Multithreading, 等級 7, 課堂 15
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
任務
Java Multithreading, 等級 7, 課堂 15
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
任務
Java Multithreading, 等級 7, 課堂 15
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
任務
Java Multithreading, 等級 7, 課堂 15
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
任務
Java Multithreading, 等級 7, 課堂 15
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
任務
Java Multithreading, 等級 7, 課堂 15
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
任務
Java Multithreading, 等級 7, 課堂 15
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
任務
Java Multithreading, 等級 7, 課堂 15
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
任務
Java Multithreading, 等級 7, 課堂 15
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
任務
Java Multithreading, 等級 7, 課堂 15
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
任務
Java Multithreading, 等級 7, 課堂 15
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
任務
Java Multithreading, 等級 7, 課堂 15
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
任務
Java Multithreading, 等級 7, 課堂 15
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
任務
Java Multithreading, 等級 7, 課堂 15
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!