This commit is contained in:
kamran 2012-11-05 22:07:10 +00:00
parent 682eb03b19
commit 13f1c4f0e0
2 changed files with 62 additions and 44 deletions

View File

@ -216,10 +216,7 @@ public class TarHeader {
*/
public static TarHeader createHeader(String entryName, long size, long modTime, boolean dir) {
String name = entryName;
name = name.replace(File.separatorChar, '/');
if (name.startsWith("/"))
name = name.substring(1);
name = TarUtils.trim(name.replace(File.separatorChar, '/'), '/');
TarHeader header = new TarHeader();
header.linkName = new StringBuffer("");

View File

@ -24,52 +24,73 @@ import java.io.File;
*
*/
public class TarUtils {
/**
* Determines the tar file size of the given folder/file path
*
* @param path
* @return
*/
public static long calculateTarSize(File path) {
return tarSize( path ) + TarConstants.EOF_BLOCK;
}
/**
* Determines the tar file size of the given folder/file path
*
* @param path
* @return
*/
public static long calculateTarSize(File path) {
return tarSize(path) + TarConstants.EOF_BLOCK;
}
private static long tarSize(File dir) {
long size = 0;
private static long tarSize(File dir) {
long size = 0;
if (dir.isFile()) {
return entrySize( dir.length() );
} else {
File[] subFiles = dir.listFiles();
if (dir.isFile()) {
return entrySize(dir.length());
} else {
File[] subFiles = dir.listFiles();
if (subFiles != null && subFiles.length > 0) {
for (File file : subFiles) {
if (file.isFile()) {
size += entrySize( file.length() );
} else {
size += tarSize( file );
}
}
} else {
// Empty folder header
return TarConstants.HEADER_BLOCK;
}
}
if (subFiles != null && subFiles.length > 0) {
for (File file : subFiles) {
if (file.isFile()) {
size += entrySize(file.length());
} else {
size += tarSize(file);
}
}
} else {
// Empty folder header
return TarConstants.HEADER_BLOCK;
}
}
return size;
}
return size;
}
private static long entrySize(long fileSize) {
long size = 0;
size += TarConstants.HEADER_BLOCK; // Header
size += fileSize; // File size
private static long entrySize(long fileSize) {
long size = 0;
size += TarConstants.HEADER_BLOCK; // Header
size += fileSize; // File size
long extra = size % TarConstants.DATA_BLOCK;
long extra = size % TarConstants.DATA_BLOCK;
if (extra > 0) {
size += ( TarConstants.DATA_BLOCK - extra ); // pad
}
if (extra > 0) {
size += (TarConstants.DATA_BLOCK - extra); // pad
}
return size;
}
return size;
}
public static String trim(String s, char c) {
StringBuffer tmp = new StringBuffer(s);
for (int i = 0; i < tmp.length(); i++) {
if (tmp.charAt(i) != c) {
break;
} else {
tmp.deleteCharAt(i);
}
}
for (int i = tmp.length() - 1; i >= 0; i--) {
if (tmp.charAt(i) != c) {
break;
} else {
tmp.deleteCharAt(i);
}
}
return tmp.toString();
}
}