The following is my source code
public class CreateTarFile
{
public CreateTarFile()
{
}
private static void readTar(InputStream in, String untarDir) throws IOException
{
TarInputStream tin = new TarInputStream(in);
TarEntry tarEntry = tin.getNextEntry();
boolean test = tarEntry.isDirectory();
System.out.println("Is Directory: "+test);
if(new File(untarDir).exists())
{
while (tarEntry != null)
{
File destPath = new File(untarDir + File.separatorChar + tarEntry.getName());
System.out.println("Processing " + destPath.getAbsoluteFile());
if(!tarEntry.isDirectory())
{
FileOutputStream fout = new FileOutputStream(destPath);
tin.copyEntryContents(fout);
fout.close();
}
else
{
System.out.println("directory does not exist");
destPath.mkdir();
}
tarEntry = tin.getNextEntry();
}
tin.close();
}
else
{
System.out.println("That destination directory doesn't exist! " + untarDir);
}
}
/**
* Add a file to a Tar OutputStream
*/
private static boolean addToTar(TarOutputStream tos, File inFile)
{
try
{
//System.out.println("Debug: " + inFile.toString());
if ( inFile.exists() && inFile.isFile())
{
// Create a file input stream and a buffered input stream.
FileInputStream fis = new FileInputStream(inFile);
BufferedInputStream bis = new BufferedInputStream(fis);
// Create a Tar Entry and put it into the archive (no data yet).
String filePath = inFile.getPath();
System.out.println("filePath: "+filePath);
filePath = filePath.substring(filePath.indexOf(File.separatorChar)+1,filePath.length());
filePath = filePath.substring(filePath.indexOf(File.separatorChar)+1,filePath.length());
filePath = filePath.substring(filePath.indexOf(File.separatorChar)+1,filePath.length());
System.out.println("filePath: "+filePath);
// TarEntry fileEntry = new TarEntry((File) inFile);
TarEntry fileEntry = new TarEntry(filePath);
fileEntry.setSize(inFile.length());
tos.putNextEntry(fileEntry);
// Create a byte array object named data and declare byte count variable.
byte[] data = new byte[1024];
int byteCount;
// Create a loop that reads from the buffered input stream and writes
// to the tar output stream until the bis has been entirely read.
while ((byteCount = bis.read(data)) > -1)
{
tos.write(data, 0, byteCount);
}
tos.closeEntry();
return true;
}
return false;
}
catch (IOException exception)
{
exception.printStackTrace();
return false;
}
}
public static void main(String[] args)
{
try
{
TarOutputStream tos = new TarOutputStream(new FileOutputStream("D:/abc/abc.tar.gz"));
File sdir = new File("D:/abc");
File inFile = null;
File [] listFiles = sdir.listFiles();
for(int i = 0; i < listFiles.length; i++)
{
inFile = listFiles;
if(inFile.isFile())
{
addToTar(tos, inFile);
}
else
{
File [] subListFiles = inFile.listFiles();
for(int j = 0; j < subListFiles.length; j++)
{
inFile = subListFiles[j];
addToTar(tos, inFile);
}
}
}
readTar(new FileInputStream("D:/abc/abc.tar.gz"), "D:/abc/");
}
catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
I have a folder D:\abc and inside this folder I have the following files and folder
D:\abc\figure\
D:\abc\Abc.ppp
D:\abc\Abc.ttt
D:\abc\Abc.xxx
D:\abc\Abc-1.ppp
From my above source code, I am creating a D:\abc\abc.tar.gz file consisting of the above-mentioned files and folder. But the created D:\abc\abc.tar.gz is *0 KB*.
Could anyone please tell me where am I wrong in my above source code.