write compression and de-compression programs for the fol- lowing file: from dir
ID: 3695043 • Letter: W
Question
write compression and de-compression programs for the fol- lowing file:
from directory (small characters, the link is long)
This file has two types of lines: scores and starts. A score is a single number, a multiple of 1/1000 that is between 0 and 1. A start line looks like that:
Importantly, there are only 62,290 lines of the second type, so the compression strategy should focus on the rest of 142,782,023 lines. The second type of lines can be easily encoded with and extra symbol, say "f", followed by four bytes that encode the start value.
Using the posted script, one can compute that the average length of Huffman codes for score lines as 6.68. However, the scores are not random but they tend to be similar to previous scores. If we encode the differences (between -1 and 1), the average code is 5 bits, even though we have 2001 codes rather tnan 1001.
You task is to write a pair of programs, one that compresses and produces under 100 Mbytes, and one that decompresses and produces the original file of 850 Mbytes. Compare the compressed size and the running time with zip or gzip.
Explanation / Answer
CODE FOR Compression
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class compress
{
public static void main( String[] args )
{
byte[] bfrr = new byte[1024];
try{
FileOutputStream fss = new FileOutputStream(" chr1.phastCons60way.wigFix.gz");
ZipOutputStream zss = new ZipOutputStream(fss);
ZipEntry ze= new ZipEntry("yourfilename");
zss.putNextEntry(ze);
FileInputStream in = new FileInputStream("yourfilename");
int len;
while ((len = in.read(bfrr)) > 0) {
zss.write(bfrr, 0, len);
}
in.close();
zss.closeEntry();
//remember close it
zss.close();
System.out.println("Done");
}catch(IOException ex){
ex.printStackTrace();
}
}
}
CODE For Decompression:
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.zpinputStream;
public class unzpUtility {
private static final int bfr_size = 4096;
public void unzp(String zppath, String dstdirectry) throws IOException {
File destDir = new File(dstdirectry);
if (!destDir.exists()) {
destDir.mkdir();
}
zpinputStream zpin = new zpinputStream(new FileInputStream(zppath));
ZipEntry entry = zpin.getNextEntry();
// iterates over entries in the zip file
while (entry != null) {
String filePath = dstdirectry + File.separator + entry.getName();
if (!entry.isDirectory()) {
// if the entry is a file, extracts it
extrtfile(zpin, filePath);
} else {
// if the entry is a directory, make the directory
File dir = new File(filePath);
dir.mkdir();
}
zpin.closeEntry();
entry = zpin.getNextEntry();
}
zpin.close();
}
private void extrtfile(zpinputStream zpin, String filePath) throws IOException {
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));
byte[] bytesIn = new byte[bfr_size];
int read = 0;
while ((read = zpin.read(bytesIn)) != -1) {
bos.write(bytesIn, 0, read);
}
bos.close();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.