COMP2100/2500
Homework 9: Extracting from a Simple ArchiveContinue filling in a new Time Recording Log and Weekly Time Use Summary each week.
Write the following program, following the enhanced PSP as described in lectures, and filling in the Project Plan Summary and a Defect Recording Log. Use one of the Project Plan Summary forms that doesn't have greyed-out sections.
Write a Java program called ‘extract’ that extract files that have been packed into a simple archive.
In the words of the manual entry for a more advanced archive program
an archive is a single file holding a collection of other files in a structure that makes it possible to retrieve the original individual files (called members of the archive).
[from the manual entry on the Unix command
ar]Program arguments
The
extractprogram shall take exactly one command line argument, which should be a filename (anywhere in the local filesystem). If the file does not exist or cannot be opened (lack of access permission) then print an error message and stop. The file is expected to be a simple archive file in the format defined below.Program output
The
extractprogram shall create (or overwrite) files in the current directory with the members of the simple archive file named on the command line.Simple Archive format
The format of a simple archive file is due to Smith [Michael A Smith, Object-Oriented Software in C++, Chapman & Hall 1993, section 16.3]. The simple archive file contains one or more “archive chunks”. Each chunk contains the name of the archive member followed by its contents. The chunk starts with a line of text in the form
@@ filenameN @@A chunk ends with the start of the next chunk, or at the end of the file. A simple archive file containing 2 member files therefore looks like:@@ filename1 @@ line 1 of file 1 line 2 of file 1 ... last line of file 1 @@ filename2 @@ line 1 of file 2 line 2 of file 2 ... last line of file 2 (end of file)The members are the files named “filename1” and “filename2”.Error conditions
Detect and report at least the following errors:
- no start of chunk, or malformed start of chunk (e.g. no filename between the @@ markers) at the start of the file (any other lines are either the start of a new chunk – or a line of contents in the first chunk, even if this line starts with @@).
- unable to open or write to member filename
Hints and suggestions
This program will need to open an input file and read it line by line. (you could read character by character, but it is essentially a line-oriented program, so reading lines is recommended). It will need to open many output files, one at a time, and write a line read from the input into an output file. You will need to close each output file (to ensure that any buffered output is written out to the filesystem)and open new ones. Debugging during development may be easier if you start by reading the input and recognising the chunk header lines, and reporting them to the console output, before you start to worry about opening and writing to the output files.
It is useful to write a method that decides whether a line of text is a start of chunk i.e. a method that implements checking the syntax of a chunk-header. It may be useful to have this or another method return the filename from a correctly-formed chunk-header line of text.
Test files and creating archive files for testing
One sample archive file is supplied here.
A shell script I have called archive.sh can be used to create more examples. It accepts any number of filenames, and outputs the archive to standards output. If you want to create this into an archive file then redirect the shell output using ">".
Usage: execute the script with a list of filenames (wildcards allowed). For example to create an archive called "fred.sr" containing 3 files named file1, file2, and file3:./archive.sh file1 file2 file3 > fred.sror equivalently (assuming there are no other files with names that start with "fred")./archive.sh file* > fred.srI have invented a new program extension.srto mean "simple archive".Extension 1a: input file chooser
Use your solution as the base for a GUI-driven version. The program shall provide a file chooser widget that allows the user to browse and choose the archive file from anywhere in the file system.
Use JFileChooser.
Extension 1b: output directory file chooser
Add a second file chooser that chooses the directory where the output files will be output. If the directory does not exist allow the user to decide whether to create a new directory of that name, or continue browsing to find an output directory (use a button to control this).
Extension 2
Write a java version of the
archiveshell script that accepts many filename arguments and creates an output simple archive file. Allow the first filename in the list to be the name of a new archive.Copyright © 2009, Chris Johnson, The Australian National University
$Revision: 1.5 $ $Date: 2009/05/13 08:31:56 $ $Author: cwj $ Feedback & Queries to comp2100@cs.anu.edu.au