Code improvements in Common Utils
[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 public class Gunzip {
22
23         public void unCompress(String gzFileName, String toFile) throws IOException {
24                 try (   FileInputStream gzInput = new FileInputStream(gzFileName);
25                         GZIPInputStream gzIn = new GZIPInputStream(gzInput)){
26                         File tofile = new File(toFile);
27                         enable(tofile);
28                         try(FileOutputStream fileOutput = new FileOutputStream(tofile, false)){
29                                 moveBytes(gzIn, fileOutput, -1, -1, 1024);
30                         } 
31                 }catch(IOException e){
32                         throw new IOException ("Gunzip", e);
33                 }
34
35         }
36
37         private void enable(File tofile) throws IOException {
38                 if (!tofile.exists()) {
39                         String parentPath = tofile.getParent();
40                         if (parentPath != null)
41                                 new File(parentPath).mkdirs();
42                         tofile.createNewFile();
43                 }
44         }
45
46         public long moveBytes(InputStream input, OutputStream output, long off, long len, int bufsize) throws IOException {
47                 /*      long skipped=0;
48                         if (off > 0)
49                         skipped = input.skip(off); // check if skipped is same as off
50                 */
51                 long totalNum = 0;
52                 byte[] buf = new byte[bufsize];
53
54                 while (true) {
55                         if (len > 0 && (len - totalNum) <= 0)
56                                 break;
57
58                         else if (len > 0 && bufsize > (len - totalNum))
59                                 bufsize = (int) (len - totalNum);
60
61                         int readNum = input.read(buf, 0, bufsize);
62                         if (readNum <= 0)
63                                 break;
64
65                         output.write(buf, 0, readNum);
66                         totalNum += readNum;
67                 }
68                 return totalNum;
69         }
70
71 }