2 * Copyright 2012 Kamran Zafar
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org.kamranzafar.jtar;
26 public class TarUtils {
28 * Determines the tar file size of the given folder/file path
33 public static long calculateTarSize(File path) {
34 return tarSize(path) + TarConstants.EOF_BLOCK;
37 private static long tarSize(File dir) {
41 return entrySize(dir.length());
43 File[] subFiles = dir.listFiles();
45 if (subFiles != null && subFiles.length > 0) {
46 for (File file : subFiles) {
48 size += entrySize(file.length());
50 size += tarSize(file);
54 // Empty folder header
55 return TarConstants.HEADER_BLOCK;
62 private static long entrySize(long fileSize) {
64 size += TarConstants.HEADER_BLOCK; // Header
65 size += fileSize; // File size
67 long extra = size % TarConstants.DATA_BLOCK;
70 size += (TarConstants.DATA_BLOCK - extra); // pad
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) {
86 for (int i = tmp.length() - 1; i >= 0; i--) {
87 if (tmp.charAt(i) != c) {
94 return tmp.toString();