compression 的infile就是原先的tar起來的file, outfile就是compression放置的地方
decompression的動作就是相反.
- /**
- * the input is the file that we want to compression
- * the output is the compressed file
- * @param infile
- * @param outfile
- * @return
- * @throws IOException
- */
- BufferedInputStream fin = null;
- BufferedOutputStream fout = null;
- try{
- byte[] buffer = new byte[1024];
- int offset = -1;
- while( (offset = fin.read(buffer)) != -1){
- zout.write(buffer, 0, offset);
- }
- zout.closeEntry();
- byte[] compressed = out.toByteArray();
- zout.close();
- fout.write(compressed);
- fout.close();
- }
- }
- finally{
- if(fin != null){
- fin.close();
- }
- if(fout != null){
- fout.flush();
- fout.close();
- }
- }
- }
- /**
- * the input is the compressed file
- * the output is the original file
- * @param compressed
- * @return
- * @throws IOException
- */
- int read=0;
- byte[] buffer = new byte[1024];
- BufferedInputStream fin = null;
- BufferedWriter fout = null;
- ZipInputStream zin = null;
- ByteArrayOutputStream out = null;
- try{
- int offset = -1;
- while((offset = zin.read(buffer)) != -1) {
- out.write(buffer, 0, offset);
- }
- fout.write(out.toString());
- }
- }
- finally{
- if(fin != null)
- fin.close();
- if(fout != null){
- fout.flush();
- fout.close();
- }
- if(out != null)
- out.close();
- if(zin != null)
- zin.close();
- }
- }
沒有留言:
張貼留言