CodeGym/课程/Java 多线程/大任务:用 Java 编写归档程序

大任务:用 Java 编写归档程序

可用

“嗨,阿米戈!”

“您好,松鼠船长,先生!”

“准备好接受新的秘密任务了吗?”

“当然,我准备好了,长官!”

“那就来一份文件,里面有说明,今天我们要研发一种新型的人工智能,人类需要我们的帮助,我们必须拯救人类免于毁灭。”

“但是,先生!我无法打开文件。我需要一个存档器。”

“是吗?嗯……那人类的救赎就延期了,今天,我们自己写存档。”

大任务:用 Java 编写归档器 - 1

“队长,那些人呢?”

“他们不会有事的。联系 IntelliJ IDEA 特工来完成你的任务。他会给你所有的指示。”

“我可以继续吗,先生?”

“继续。”

16
任务
Java 多线程,  第 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 多线程,  第 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 多线程,  第 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 多线程,  第 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 多线程,  第 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 多线程,  第 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 多线程,  第 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 多线程,  第 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 多线程,  第 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 多线程,  第 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 多线程,  第 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 多线程,  第 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 多线程,  第 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 多线程,  第 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 多线程,  第 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 多线程,  第 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 多线程,  第 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 多线程,  第 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!
评论
  • 受欢迎
你必须先登录才能发表评论
此页面还没有任何评论