Creating a TAR file using JTar library - java

I have been trying to create a tar file from JAVA using the JTar library.I am trying to pack two files into one tar file.However the second file doesn't get listed in the created tar.Rather it is showing up as the content of the first file.
My code looks like :
// Output file stream
FileOutputStream dest = new FileOutputStream("C:\\tarFile");
// Create a TarOutputStream
TarOutputStream out = new TarOutputStream(new BufferedOutputStream(dest));
// Files to tar
File[] filesToTar = new File[2];
filesToTar[0] = new File("C:\\tarSample\\File1.txt");
filesToTar[1] = new File("C:\\tarSample\\File2.txt");
for (File f : filesToTar)
{
TarEntry entry = new TarEntry(f);
out.putNextEntry(entry);
BufferedInputStream origin = new BufferedInputStream(new FileInputStream(f));
int count;
byte data[] = new byte[2048];
while ((count = origin.read(data)) != -1)
{
out.write(data, 0, count);
}
out.flush();
origin.close();
}
out.close();
dest.close();
When i open the contents of the tarFile using "cat" command it looks like :
tarSample/File1.txt100644 0 0 10 12301634500 13305 0ustarkumarang 0 0 tarSample/File2.txt100644 0 0 7 12301634511 13276 0ustarkumarang 0 0
If my understanding is correct the tarEntry is getting added to the tar file .However the contents of the files are not getting written.
Any body knows a fix ?
Thanks.

Just change it to
FileOutputStream dest = new FileOutputStream("C:\\tarFile.tar");

Related

Create a directory with ./ folder in java on Windows OS

I am working on a Java project in which the Tar file has to be created. These tar files are created on Linux OS so the folder structure always as : C:\Users\Admin\Desktop\Tar\My.tar\.\
In this path, the files are kept i.e. if I open My.tar using 7z or anything then I see files within the path : C:\Users\Admin\Desktop\Tar\My.tar\.\
Now on windows, I have written the code to generate tar file with some files. But it does not create folder having ./ I want the same structure to be followed to generate tar so that existing code wor fine.
How to create a directory with ./ - C:\Users\Admin\Desktop\Tar\My.tar\.\
The code to generate tar file is :
FileOutputStream dest = new FileOutputStream(outputTarFileName);
// Create a TarOutputStream
TarOutputStream out = new TarOutputStream(new BufferedOutputStream(dest));
File folder = new File(inputFolder);
File[] filesToTar = new File[folder.listFiles().length];
int index = 0;
for (final File fileEntry : folder.listFiles()) {
filesToTar[index++] = new File((fileEntry.getAbsolutePath()));
}
for (File f : filesToTar) {
out.putNextEntry(new TarEntry(f, f.getName()));
BufferedInputStream origin = new BufferedInputStream(new FileInputStream(f));
int count;
byte data[] = new byte[2048];
while ((count = origin.read(data)) != -1) {
out.write(data, 0, count);
}
out.flush();
origin.close();
}
out.close();
createTarFile("C:\\Users\\Admin\\Desktop\\Tar\\MyDir","C:\\Users\\Admin\\Desktop\\Tar\\\\.\\My.tar");
I tried by creating a directory having such structure :
new File("C:\Users\Admin.000\Desktop\testfolder\./\W").mkdir();
new File("C:\Users\Admin.000\Desktop\testfolder\\.\W").mkdir();
But nothing worked.

How to create ZIP files using list of Input streams?

In my case I have to download images from the resources folder in my web app. Right now I am using the following code to download images through URL.
url = new URL(properties.getOesServerURL() + "//resources//WebFiles//images//" + imgPath);
filename = url.getFile();
is = url.openStream();
os = new FileOutputStream(sClientPhysicalPath + "//resources//WebFiles//images//" + imgPath);
b = new byte[2048];
while ((length = is.read(b)) != -1) {
os.write(b, 0, length);
}
But I want a single operation to read all images at once and create a zip file for this.
I don't know so much about the use of sequence input streams and zip input streams so if it is possible through these, please let me know.
The only way I can see you being able to do this is something like the following:
try {
ZipOutputStream zip = new ZipOutputStream(new FileOutputStream("C:/archive.zip"));
//GetImgURLs() is however you get your image URLs
for(URL imgURL : GetImgURLs()) {
is = imgURL.openStream();
zip.putNextEntry(new ZipEntry(imgURL.getFile()));
int length;
byte[] b = new byte[2048];
while((length = is.read(b)) > 0) {
zip.write(b, 0, length);
}
zip.closeEntry();
is.close();
}
zip.close();
}
Ref: ZipOutputStream Example
The url should return zip file. Else you have to take one by one and create a zip using your program

Can't Retrieve Resources from External Jar File

NOTE: This is a followup to my question here.
I have a program that takes the contents of a directory and bundles everything into a JAR file. The code I use to do this is here:
try
{
FileOutputStream stream = new FileOutputStream(target);
JarOutputStream jOS = new JarOutputStream(stream);
LinkedList<File> fileList = new LinkedList<File>();
buildList(directory, fileList);
JarEntry jarAdd;
String basePath = directory.getAbsolutePath();
byte[] buffer = new byte[4096];
for(File file : fileList)
{
String path = file.getPath().substring(basePath.length() + 1);
path.replaceAll("\\\\", "/");
jarAdd = new JarEntry(path);
jarAdd.setTime(file.lastModified());
jOS.putNextEntry(jarAdd);
FileInputStream in = new FileInputStream(file);
while(true)
{
int nRead = in.read(buffer, 0, buffer.length);
if(nRead <= 0)
break;
jOS.write(buffer, 0, nRead);
}
in.close();
}
jOS.close();
stream.close();
So, all is well and good and the jar gets created, and when I explore its contents with 7-zip it has all the files I need. However, when I try to access the contents of the Jar via a URLClassLoader (the jar is not on the classpath and I don't intend it to be), I get null pointer exceptions.
The odd thing is, when I use a Jar that I've exported from Eclipse, I can access the contents of it in the way I want. This leads me to believe that I'm somehow not creating the Jar correctly, and am leaving something out. Is there anything missing from the method up above?
I figured it out based on this question - the problem was me not properly handling backslashes.
Fixed code is here:
FileOutputStream stream = new FileOutputStream(target);
JarOutputStream jOS = new JarOutputStream(stream);
LinkedList<File> fileList = new LinkedList<File>();
buildList(directory, fileList);
JarEntry entry;
String basePath = directory.getAbsolutePath();
byte[] buffer = new byte[4096];
for(File file : fileList)
{
String path = file.getPath().substring(basePath.length() + 1);
path = path.replace("\\", "/");
entry = new JarEntry(path);
entry.setTime(file.lastModified());
jOS.putNextEntry(entry);
FileInputStream in = new FileInputStream(file);
while(true)
{
int nRead = in.read(buffer, 0, buffer.length);
if(nRead <= 0)
break;
jOS.write(buffer, 0, nRead);
}
in.close();
jOS.closeEntry();
}
jOS.close();
stream.close();

separate multiple jpg files from a single file

I have merged multiple jpeg files into one single .bin file.
.....
.........
while(true){
if (q.numOfFiles() > 0) {
source = q.getNextFile();
in = new DataInputStream(new BufferedInputStream(new FileInputStream(source)));
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
String s = "filename=="+source.getName()+"==filename";
out.write(s.getBytes());
out.flush();
System.out.println("merged--"+source.getName());
}
}
........
........
as you can see i am appending "filename=="+source.getName()+"==filename" after end of each file.
Now i want to separate all those jpegs with their actual file names.
How can i read the separators that I've inserted in the merged files ?
As above in the comments:
Sugestion/answer to adapt your point of view:
Using a single file zip to ship multiple files.
How to create zip in Java
And to extract:
Extract from zip in Java

Unzipping Files with a large amount of small Files inside

I have a lot of zips with a large amount of Files which i have to unzip to the SD-Card.
Currently I am using the util.zip Package with Zipentry for each File inside the zips. This works fine but is very slow.
So I wonder if there is a lib which can handle those Files faster than the normal zip of Java/Android.
Edit: The zips are archives which are about 5 to 10MB large and contain about 50 to 100 jpg-Files whiche are each part of a picture. I need to extract all zips to a specific folder.
Slow means that the same files are extracted on an IPhone in fraction of the time.
Edit 2 the code:
ZipInputStream zin = new ZipInputStream(fd);
ZipEntry ze = null;
while ((ze = zin.getNextEntry()) != null) {
System.out.println("Unzipping " + ze.getName());
if (ze.isDirectory()) {
File f = new File(targetFilePath + ze.getName());
if (!f.isDirectory()) {
f.mkdirs();
}
} else {
int size;
byte[] buffer = new byte[2048];
FileOutputStream outStream = new FileOutputStream(targetFilePath + ze.getName() + ".tile");
BufferedOutputStream bufferOut = new BufferedOutputStream(outStream, buffer.length);
while ((size = zin.read(buffer, 0, buffer.length)) != -1) {
bufferOut.write(buffer, 0, size);
}
bufferOut.flush();
bufferOut.close();
}
}
zin.close();
The best case you could do is go NDK way. AFAIK, Google even provides supported headers/API in NDK on zlib.

Categories

Resources