]> WPIA git - gigi.git/blob - lib/jtar/org/kamranzafar/jtar/TarUtils.java
Merge branch 'libs/jetty/upstream' into libs/jetty/local
[gigi.git] / lib / jtar / org / kamranzafar / jtar / TarUtils.java
1 /**
2  * Copyright 2012 Kamran Zafar 
3  * 
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  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0 
9  * 
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  */
17
18 package org.kamranzafar.jtar;
19
20 import java.io.File;
21
22 /**
23  * @author Kamran
24  * 
25  */
26 public class TarUtils {
27         /**
28          * Determines the tar file size of the given folder/file path
29          * 
30          * @param path
31          * @return
32          */
33         public static long calculateTarSize(File path) {
34                 return tarSize(path) + TarConstants.EOF_BLOCK;
35         }
36
37         private static long tarSize(File dir) {
38                 long size = 0;
39
40                 if (dir.isFile()) {
41                         return entrySize(dir.length());
42                 } else {
43                         File[] subFiles = dir.listFiles();
44
45                         if (subFiles != null && subFiles.length > 0) {
46                                 for (File file : subFiles) {
47                                         if (file.isFile()) {
48                                                 size += entrySize(file.length());
49                                         } else {
50                                                 size += tarSize(file);
51                                         }
52                                 }
53                         } else {
54                                 // Empty folder header
55                                 return TarConstants.HEADER_BLOCK;
56                         }
57                 }
58
59                 return size;
60         }
61
62         private static long entrySize(long fileSize) {
63                 long size = 0;
64                 size += TarConstants.HEADER_BLOCK; // Header
65                 size += fileSize; // File size
66
67                 long extra = size % TarConstants.DATA_BLOCK;
68
69                 if (extra > 0) {
70                         size += (TarConstants.DATA_BLOCK - extra); // pad
71                 }
72
73                 return size;
74         }
75
76         public static String trim(String s, char c) {
77                 StringBuffer tmp = new StringBuffer(s);
78                 for (int i = 0; i < tmp.length(); i++) {
79                         if (tmp.charAt(i) != c) {
80                                 break;
81                         } else {
82                                 tmp.deleteCharAt(i);
83                         }
84                 }
85
86                 for (int i = tmp.length() - 1; i >= 0; i--) {
87                         if (tmp.charAt(i) != c) {
88                                 break;
89                         } else {
90                                 tmp.deleteCharAt(i);
91                         }
92                 }
93
94                 return tmp.toString();
95         }
96 }