DataInputStream in = null;
DataOutputStream out = null;
try {
// setup in
PositionTrackingInputStream ptis = new PositionTrackingInputStream(
new FileInputStream(new File(inputFile)));
in = new DataInputStream(ptis);
// read header information
int imgVersion = in.readInt();
if (!LayoutVersion.supports(Feature.FSIMAGE_COMPRESSION, imgVersion)) {
System.out
.println("Image is not compressed. No output will be produced.");
return;
}
int namespaceId = in.readInt();
long numFiles = in.readLong();
long genstamp = in.readLong();
long imgTxId = -1;
if (LayoutVersion.supports(Feature.STORED_TXIDS, imgVersion)) {
imgTxId = in.readLong();
}
FSImageCompression compression = FSImageCompression
.readCompressionHeader(new Configuration(), in);
if (compression.isNoOpCompression()) {
System.out
.println("Image is not compressed. No output will be produced.");
return;
}
in = BufferedByteInputStream.wrapInputStream(
compression.unwrapInputStream(in), FSImage.LOAD_SAVE_BUFFER_SIZE,
FSImage.LOAD_SAVE_CHUNK_SIZE);
System.out.println("Starting decompression.");
// setup output
out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(
outputFile)));
// write back the uncompressed information
out.writeInt(imgVersion);
out.writeInt(namespaceId);
out.writeLong(numFiles);
out.writeLong(genstamp);
if (LayoutVersion.supports(Feature.STORED_TXIDS, imgVersion)) {
out.writeLong(imgTxId);
}
// no compression
out.writeBoolean(false);
// copy the data
long size = new File(inputFile).length();
// read in 1MB chunks
byte[] block = new byte[1024 * 1024];
while (true) {
int bytesRead = in.read(block);
if (bytesRead <= 0)
break;
out.write(block, 0, bytesRead);
printProgress(ptis.getPos(), size);
}
out.close();
long stop = System.currentTimeMillis();