Skip to main content



      Home
Home » Eclipse Projects » Eclipse Platform » Writing to a tar file
Writing to a tar file [message #275982] Mon, 15 November 2004 12:48
Eclipse UserFriend
Originally posted by: derek.p.gilbert.intel.com

I have written the following tar utility which should create a tar archive
of a directory. I am running this on windows. I am getting some very
strange behavior in that the tar file output only contains the first file
in the directory. The file then contains part of the path for the next
file up to the byte length of the data it originally contained in the
source file. I having this exact utility working for ZIP and I slightly
modified it to work for TAR. Can anyone provide some elightening as to
what may cause this problem. I can find no working examples of a java tar
utility anywhere except for the ICE tar.

I am calling this tar utility from this JUNIT test within Eclipse:

public void testTarGzFile()
{
File tarFileSource = new File("P:\\MyDocs\\junk\\testZip");
String destFileSource = "P:\\MyDocs\\junk\\test\\tar\\";

try
{
FileUtility.tarGzFile(tarFileSource, destFileSource);
assertTrue(true);
} catch (IOException e)
{
assertTrue(false);
}
}

// Here is the tar utility

public static void tarGzFile(File source, String dest)
throws IOException
{
FileOutputStream fos =
new FileOutputStream(
addFinalSeparator(dest) + source.getName() + ".tar");
TarOutputStream tos =
new TarOutputStream(new BufferedOutputStream(fos));

tarDir(source.toString(), tos);
tos.close();
}

public static void tarDir(String dir2zip, TarOutputStream tos)
{
try
{
final int BUFFER = 2048;
File tarDirectory = new File(dir2zip);

//get a listing of the directory content
int bytesIn = 0;
byte[] tarBuffer = new byte[BUFFER];
String[] dirList = tarDirectory.list();

//loop through directory and zip the files
for(int ii=0; ii<dirList.length; ii++)
{
File curTarFile = new File(tarDirectory, dirList[ii]);
if(curTarFile.isDirectory())
{
String filePath = curTarFile.getPath();
tarDir(filePath, tos);
continue;
}

FileInputStream fis = new FileInputStream(curTarFile);

TarEntry tarEntry = new TarEntry(curTarFile.getPath());
tarEntry.setModTime(curTarFile.lastModified());
tarEntry.setSize(curTarFile.length());
tos.putNextEntry(tarEntry);
System.out.println(curTarFile);

//write the content of the file to the TarOutputStream
while((bytesIn = fis.read(tarBuffer)) != -1)
{
tos.write(tarBuffer, 0, bytesIn);
}

fis.close();
}
}
catch(Exception e)
{
//handle exception
}
}
Previous Topic:Opening standard email client composer window with attachments
Next Topic:CVS: knowing which branch is a revision from
Goto Forum:
  


Current Time: Sat Jul 19 20:49:29 EDT 2025

Powered by FUDForum. Page generated in 0.04466 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top