add test case
[vfc/nfvo/driver/ems.git] / ems / boco / src / main / java / org / onap / vfc / nfvo / emsdriver / commons / utils / Gunzip.java
1 package org.onap.vfc.nfvo.emsdriver.commons.utils;
2
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.io.FileOutputStream;
6 import java.io.IOException;
7 import java.io.InputStream;
8 import java.io.OutputStream;
9 import java.util.zip.GZIPInputStream;
10
11
12 public class Gunzip {
13         
14         /**
15          * 
16          */
17         public void unCompress(String gzFileName, String toFile) 
18                 throws IOException {
19                 GZIPInputStream gzIn = null;
20                 FileOutputStream fileOutput = null;
21                 FileInputStream gzInput = new FileInputStream(gzFileName);
22                 try {
23                         gzIn = new GZIPInputStream(gzInput);
24                         File tofile = new File(toFile);
25                         enable(tofile);
26                         fileOutput = new FileOutputStream(tofile, false);
27
28                         moveBytes(gzIn, fileOutput, -1, -1, 1024);
29                 } finally{
30                         if(gzIn != null){
31                                 gzIn.close();
32                         }
33                         if(fileOutput != null){
34                                 fileOutput.close();
35                         }
36                         
37                 }
38                 
39                 
40         }
41         
42         private void enable(File tofile) throws IOException {
43                 if (!tofile.exists()) {
44                         String parentPath = tofile.getParent();
45                         if (parentPath != null)
46                                 new File(parentPath).mkdirs();
47                         tofile.createNewFile();
48                 }
49         }
50
51
52         public long moveBytes(InputStream input, OutputStream output, long off, long len, int bufsize) 
53                         throws IOException {
54                         if (off > 0)
55                                 input.skip(off);
56                         
57                         long totalNum = 0;
58                         byte[] buf = new byte[bufsize];
59
60                         while (true) {
61                                 if (len>0 && (len-totalNum)<=0)
62                                         break;
63                                 
64                                 else if (len>0 && bufsize>(len-totalNum))
65                                         bufsize = (int)(len-totalNum);
66                                 
67                                 int readNum = input.read(buf, 0, bufsize);
68                                 if (readNum <= 0)
69                                         break;
70                                 
71                                 output.write(buf, 0, readNum);
72                                 totalNum += readNum;
73                         }
74                         buf = null;
75                         return totalNum;
76                 }
77         
78         
79 }