bd6b1d1f9d7e1502558f034d39121190ac2c9ec0
[vfc/nfvo/driver/ems.git] / ems / boco / src / main / java / org / onap / vfc / nfvo / emsdriver / commons / utils / Gunzip.java
1 /**
2  * Copyright 2017 CMCC Technologies Co., Ltd
3  * <p>
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * <p>
8  * http://www.apache.org/licenses/LICENSE-2.0
9  * <p>
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.onap.vfc.nfvo.emsdriver.commons.utils;
17
18 import java.io.*;
19 import java.util.zip.GZIPInputStream;
20
21
22 public class Gunzip {
23
24     /**
25      *
26      */
27     public void unCompress(String gzFileName, String toFile)
28             throws IOException {
29         GZIPInputStream gzIn = null;
30         FileOutputStream fileOutput = null;
31         FileInputStream gzInput = new FileInputStream(gzFileName);
32         try {
33             gzIn = new GZIPInputStream(gzInput);
34             File tofile = new File(toFile);
35             enable(tofile);
36             fileOutput = new FileOutputStream(tofile, false);
37
38             moveBytes(gzIn, fileOutput, -1, -1, 1024);
39         } finally {
40             if (gzIn != null) {
41                 gzIn.close();
42             }
43             if (fileOutput != null) {
44                 fileOutput.close();
45             }
46
47         }
48
49
50     }
51
52     private void enable(File tofile) throws IOException {
53         if (!tofile.exists()) {
54             String parentPath = tofile.getParent();
55             if (parentPath != null)
56                 new File(parentPath).mkdirs();
57             tofile.createNewFile();
58         }
59     }
60
61
62     public long moveBytes(InputStream input, OutputStream output, long off, long len, int bufsize)
63             throws IOException {
64         if (off > 0)
65             input.skip(off);
66
67         long totalNum = 0;
68         byte[] buf = new byte[bufsize];
69
70         while (true) {
71             if (len > 0 && (len - totalNum) <= 0)
72                 break;
73
74             else if (len > 0 && bufsize > (len - totalNum))
75                 bufsize = (int) (len - totalNum);
76
77             int readNum = input.read(buf, 0, bufsize);
78             if (readNum <= 0)
79                 break;
80
81             output.write(buf, 0, readNum);
82             totalNum += readNum;
83         }
84         buf = null;
85         return totalNum;
86     }
87
88
89 }