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
Related
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");
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
I am replacing a file into the existing jar file using the java. While i am replacing the new file, remaining files modified time also changing to current timestamp. Please advise what need to do retain the original modified datetime for the other files. I am using the code,
while (entry != null) {
String name = entry.getName();
boolean notInFiles = true;
for (File f : files) {
if (f.getName().equals(name)) {
notInFiles = false;
break;
}
}
if (notInFiles) {
// Add ZIP entry to output stream.
out.putNextEntry(new ZipEntry(name));
// Transfer bytes from the ZIP file to the output file
int len;
while ((len = zin.read(buf)) > 0) {
out.write(buf, 0, len);
}
}
entry = zin.getNextEntry();
}
// Close the streams
zin.close();
// Compress the files
for (int i = 0; i < files.length; i++) {
InputStream in = new FileInputStream(files[i]);
// Add ZIP entry to output stream.
out.putNextEntry(new ZipEntry(files[i].getName()));
// Transfer bytes from the file to the ZIP file
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
// Complete the entry
out.closeEntry();
in.close();
}
As Jar-files are basicly zipfiles with a different file-type extension, you can mount the jar-file as a java.nio.file.FileSystem using ZipFileSystemProvider
Then use Files.copy(fromPath,toPath,replace) to copy the file. This will leave other files in the jar untouched, and will likely be a lot faster.
public class Zipper {
public static void main(String [] args) throws Throwable {
Path zipPath = Paths.get("c:/test.jar");
//Mount the zipFile as a FileSysten
try (FileSystem zipfs = FileSystems.newFileSystem(zipPath,null)) {
//Setup external and internal paths
Path externalTxtFile = Paths.get("c:/test1.txt");
Path pathInZipfile = zipfs.getPath("/test1.txt");
//Copy the external file into the zipfile, Copy option to force overwrite if already exists
Files.copy(externalTxtFile, pathInZipfile,StandardCopyOption.REPLACE_EXISTING);
//Close the ZipFileSystem
zipfs.close();
}
}
}
I could handle the Modified (lastModified) attribute, meaning in the archive I could preserve the Modified attribute of the file.
Here is a sample:
ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream( new FileOutputStream(outFilename)));
File f = new File(filename);
long longLastMod = f.lastModified();
FileInputStream in = new FileInputStream(filename);
// Add ZIP entry to output stream.
ZipEntry ze = new ZipEntry(filename);
ze.setTime(longLastMod); // the "magic" to store the Modified date/time of the file
out.putNextEntry( ze );
// Transfer bytes from the file to the ZIP file
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
out.closeEntry();
in.close();
out.close();
Now, the output Zip file will preserve the Modified attribute but it will not preserve the Created or Accessed attributes.
Is there a way to accomplish this?
Is there a way to accomplish this?
No it's not possible. To put it simply: the zip directory doesn't support the attributes.
What you can do, however, is using setExtra(byte[]) and store whatever information you need there. Unfortunately, you'd need a custom extractor to preserve the attributes.
Hope this helps!
My project has a few xml templates that were modified and added to a zip file. The problem was that the templates were all in a template folder but the expected format of the zip file was to have them straight in the root.
project hierarchy:
templates/blah.xml
expected zip file hierarchy:
blah.xml
Anyway of achieving that without moving the xml files into the root folder of the project?
Thanks
I had a brain melt down... By using different names for input and output
byte[] buf = new byte[1024];
try
{
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(targetFile));
FileInputStream in = new FileInputStream("templates/blah.xml");
out.putNextEntry(new ZipEntry("blah.xml"));
int len;
while ((len = in.read(buf)) > 0)
{
out.write(buf, 0, len);
}
out.closeEntry();
in.close();
out.close();
} catch (IOException e)
{
e.printStackTrace();
}
You can subclass ZipEntry and make it do whatever you want. However, you need to find out how the code is getting hold of the contents, and then modify that to get the correct resource.