• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to make a Tar in java  RSS feed

 
benny rusli
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hallo expert people,
i write a tar program under java but it did not work, can someone give a hint. Any Help will be appreciated. This Example should tar only directory recusively. Thanks

 
benny rusli
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry i forgot to write the error, here is the error displayed on the command line : request to write 1024 bytes exceeds size in header of 0 bytes
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"request to write 1024 bytes exceeds size in header of 0 bytes"

Well, based on this and this alone, my guess is that you need to set the size in the header to some number greater than 1024 before you write.

I guess you're using the org.apache.tools.tar package that is part of Ant? And the line numbers in the stack trace are pointing to the line that says

right? From the message, it looks like the tar tool expects you to specify the size of an entry before you write to it. I see that TarEntry has a setSize() method. I imagine you should try inserting a line like

Depending on what other messages you get, you may have to set other fields in the TarEntry before you write the contents.

I note that TarEntry has other constructors, and one of these takes a File as an argument. And the documentation says that it constructs the header for you based on the information in the file. To me, this sounds like less work for the programmer. Less work is good.

I also see that you're doing this:

You might want to check out the method listFiles() (in the File class). My experience is that if the thing you're talking about is a file, then a File object is much more useful that a String representing a file name. If you use a File rather than a String, you usually end up doing less work. Again, less work is good.
 
benny rusli
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After some changes from what you have said, i got the following error :
com.ice.tar.InvalidHeaderException : file name is greater than 100 characters,
c:\csharpzip\SharpZipLibSample\samples\DIME\DimeDataSetServiceConsumer\DimeDataSetServiceConsumer.csproj

If i use apache ant package, the same error occured too :
Exception in thread "main" java.lang.RuntimeException :
file name 'c:\csharpzip\SharpZipLibSample\samples\DIME\DimeDataSetServiceConsumer\DimeDataSetServiceConsumer.csproj' is too long <> 100 bytes)

Can someone give me a hint, how to overcome the problem, thanks very much

Note : now i use listFiles what you suggested(and recommended), is this implementation true, i mean the code.

 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Some versions of tar do not support names (paths, really) longer than 100 chars. If you've got paths longer than that, you won't be able to use a version that doesn't allow lengths > 100 chars. Fortunately there seem to be other choices. I don't see a way to use com.ice.tar for this. But it looks like org.apache.tools.tar will work. Try this:

Note also that you may want to put in some logic to modify the names a bit when you create entries. If you're tarring up everything from c:\csharpzip (as the base directory) you probably don't want to include the c:\csharpzip in the name. Because when someone untars the files, they will probably be on another machine with a different root directory. Insead you want a relative path name. Which means replacing

c:\csharpzip\SharpZipLibSample\samples\DIME\DimeDataSetServiceConsumer\DimeDataSetServiceConsumer.csproj

with

SharpZipLibSample\samples\DIME\DimeDataSetServiceConsumer\DimeDataSetServiceConsumer.csproj

[now i use listFiles what you suggested(and recommended), is this implementation true, i mean the code.

It looks correct, yes. I haven't tested it carefully though - you'll have to do that.
 
benny rusli
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
first thanks sheriff(Jim), you guide me in right paths, how can i modify the code using relative path ? (below is the modified code with relative path but still error when i untar it, all will be extracted in one directory, can you correct my fault ?)
2. Is it better to use rekursive method if i want to backup more than 100 GB(until 500 GB) for my backup programm, i am afraid that they eat much ram and cause memory defect. Or it is better to use iterative one, but more slower.
3. Is it better to save all the paths recusively in Java Collection, example List, ArrayList, Vector. But i dont know which is better, if the paths more than 1.000.000.000 which one is better ? after it, retrieve all the paths from the Collection and throw it to FileInputStream and TarEntry.

Note : -> is the changes

 
benny rusli
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello expert people,
i want to retrieve the long name of entry, how can i use the method from TarEntry : isGNULongNameEntry().
If i try "if(tarentryobject.isGNULongNameEntry())" then a error will be thrown, "invalid method declaration; return type required", ';' expected isGNULongNameEntry(). Any Help will be appreciated.
 
benny rusli
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hallo expert people,
i think, there is a bug in package org.apache.tools.tar, if i try to tar a path longer than 100 chars, it will be success with method tos.setLongFileMode(TarOutputStream.LONGFILE_GNU); , but i try to untar it again there is a following error :

java.io.FileNotFoundException: c:\tar2\BEST BYTE AP_NET Mania_ @8<5@ :>40 !60BL ViewState.htm
at java.io.FileOutputStream.open<Native Method>
at java.io.FileOutputStream.<init><FileOutputStream.java:179>
at java.io.FileOutputStream.<init><FileOutputStream.java:131>
at UntarBest.untar<UntarBest.java:48>
at UntarBest.main<UntarBest.java:11>

and i try in csharp, there is error too :
System.ArgumentException : IILEGAL characters in path.

I think, i should use method isGNULongNameEntry() from TarEntry, but i dont know how use it rightly. Here is my untar code. Any Help will be appreciated.


[code]
import java.io.*;
import java.util.*;
import java.util.zip.*;
import org.apache.tools.tar.*;

public class UntarBest
{
public static void main(String[] args) throws IOException
{
untar("c:\\t.tar","c:\\tar");
}

public static void untar(String tarfile,String extracttopath)
{
java.io.File file = null;
java.io.InputStream is = null;
java.io.BufferedInputStream bis = null;
java.io.BufferedOutputStream bos = null;
java.io.OutputStream os = null;
org.apache.tools.tar.TarInputStream tis = null;
org.apache.tools.tar.TarEntry te = null;
int buffersize = 1024;
byte[] buffer = null;
try
{
file = new java.io.File(tarfile);
is = new java.io.FileInputStream(file);
bis = new java.io.BufferedInputStream(is);
tis = new org.apache.tools.tar.TarInputStream(bis);

while((te = tis.getNextEntry()) != null)
{
String filepath = extracttopath + java.io.File.separator + te.getName();
java.io.File file2 = new java.io.File(filepath);
java.io.File dirParent = new java.io.File(file2.getParent());
System.err.println("Extracting file: " + te.getName());
dirParent.mkdirs();

if(te.isDirectory())
{
file2.mkdirs();
continue;
}
buffer = new byte[buffersize];
os = new java.io.FileOutputStream(file2);
bos = new java.io.BufferedOutputStream(os,buffersize);
tis.copyEntryContents(bos);
bos.close();
os.close();
}
bos.flush();
tis.close();
}
catch(IOException ioe)
{
System.err.println("Unhandled exception:");
ioe.printStackTrace();
}
}
}
[/code
 
Riyaz Karime
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Jim

i have added your code


it is working, thanks for that post...
Now i am facing the issue like tar file does not have directories created inside it.....

Any way to add the directory in the tar file


any help will be appreciated

cheers
Riyaz

 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, we forgot to welcome you to the Ranch earlier. Welcome now
Please use the code button; since you are new I have edited your post and you can see how much better it looks.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!