Creating default file for my application with images and text - java

I have the following problem:
I need to save information that is currently on my application, and this information consists on images (.gif and .jpg) and text. I want to save everything in one single file, because saving only the image path for these images may cause an error if the user deletes any of the images. How can I save image and text into one single file using Java?

You can zip the files under one file
Code Example:
byte[] buffer = new byte[1024];
try{
FileOutputStream fos = new FileOutputStream("C:\\MyFile.zip");
ZipOutputStream zos = new ZipOutputStream(fos);
ZipEntry ze= new ZipEntry("spy.log");
zos.putNextEntry(ze);
FileInputStream in = new FileInputStream("C:\\spy.log");
int len;
while ((len = in.read(buffer)) > 0) {
zos.write(buffer, 0, len);
}
in.close();
zos.closeEntry();
//remember close it
zos.close();
System.out.println("Done");
}catch(IOException ex){
ex.printStackTrace();
}

Related

Zip Jasper reports [duplicate]

I am trying to create a zip file of multiple image files. I have succeeded in creating the zip file of all the images but somehow all the images have been hanged to 950 bytes. I don't know whats going wrong here and now I can't open the images were compressed into that zip file.
Here is my code. Can anyone let me know what's going here?
String path="c:\\windows\\twain32";
File f=new File(path);
f.mkdir();
File x=new File("e:\\test");
x.mkdir();
byte []b;
String zipFile="e:\\test\\test.zip";
FileOutputStream fout=new FileOutputStream(zipFile);
ZipOutputStream zout=new ZipOutputStream(new BufferedOutputStream(fout));
File []s=f.listFiles();
for(int i=0;i<s.length;i++)
{
b=new byte[(int)s[i].length()];
FileInputStream fin=new FileInputStream(s[i]);
zout.putNextEntry(new ZipEntry(s[i].getName()));
int length;
while((length=fin.read())>0)
{
zout.write(b,0,length);
}
zout.closeEntry();
fin.close();
}
zout.close();
This is my zip function I always use for any file structures:
public static File zip(List<File> files, String filename) {
File zipfile = new File(filename);
// Create a buffer for reading the files
byte[] buf = new byte[1024];
try {
// create the ZIP file
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipfile));
// compress the files
for(int i=0; i<files.size(); i++) {
FileInputStream in = new FileInputStream(files.get(i).getCanonicalName());
// add ZIP entry to output stream
out.putNextEntry(new ZipEntry(files.get(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();
}
// complete the ZIP file
out.close();
return zipfile;
} catch (IOException ex) {
System.err.println(ex.getMessage());
}
return null;
}
Change this:
while((length=fin.read())>0)
to this:
while((length=fin.read(b, 0, 1024))>0)
And set buffer size to 1024 bytes:
b=new byte[1024];

How to download a dataset using java

I want to make an app that will download all the paper mentioned in the image by taking the domain as input. i have been able to download a single file from some other website but was unable to download a paper form acm digital library.
what i need to do is download the entire data set.
here is the code that i used to download a single file.
String fileName = "1.txt";
URL link = new URL("http://shayconcepts.com");
InputStream in = new BufferedInputStream(link.openStream());
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buf = new byte[2048];
int n = 0;
while (-1!=(n=in.read(buf)))
{
out.write(buf, 0, n);
}
out.close();
in.close();
byte[] response = out.toByteArray();
FileOutputStream fos = new FileOutputStream(fileName);
fos.write(response);
fos.close();
How can I modify it to download entire data set

Unable to unzip zip file created with java

I have a list of files from different locations. I create a zip file using the following the code which works without error. But when I try to unzip the file in Windows using Extract All it fails seeing unable to find any bytes, yet if I double click into the zip file itself with Windows Explorer I can see the files and individual ones can be opened and contains the correct data
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile));
for (File next : files)
{
ZipEntry zipEntry = new ZipEntry(next.getName());
zos.putNextEntry(zipEntry);
FileInputStream in = new FileInputStream(next);
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0)
{
zos.write(buf, 0, len);
}
zos.closeEntry();
in.close();
}
zos.close();
This may or may not be related but I've found using fixed byte length can lead to a loss of new line characters.
This may help:
final byte[] newLine = System.getProperty(
"line.separator").getBytes("UTF-8");
while ((line = in.readLine()) != null)
final byte[] buffer = line.getBytes("UTF-8");
out.write(buffer, 0, buffer.length);
out.write(newLine, 0, newLine.length);
}

Writing inputstream to file - no file found

I'm trying to write inputstream to a file, but it never gets written on disk, I just get the error file doesnt exist. The file I open is a drawable icluded in the project, I would like to save it to sd card. This is what I have so far:
File storagePath = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/tester");
InputStream inputStream = getResources().openRawResource(R.drawable.test);
OutputStream out = new FileOutputStream(new File(storagePath, "test.png"));
byte buffer[] = new byte[900];
int len;
while ((len = inputStream.read(buf)) > 0)
out.write(buffer, 0, len);
out.close();
inputStream.close();
Your tester directory doesn't exist. Check for it and create it if necessary before opening your FileOutputStream.

Saving BufferedImage using zip compression

How would I go about saving a BufferedImage straight to a zip file.
Here is my current code for saving my BufferedImage to a zip file but I do not know how to convert the BufferedImage to a InputStream so that it can be saved to the zip file.
If possible I need to save the BufferedImage straight from RAM without saving it to HDD first
byte[] buffer = new byte[1024];
try
{
FileOutputStream outputStream = new FileOutputStream(PathName + imageData.getFileNumber() + ".zip");
ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream);
ZipEntry imageZipOutput = new ZipEntry(imageData.getFileNumber() + ".png");
zipOutputStream.putNextEntry(imageZipOutput);
//the BufferedImage is stored in imageData.getImage();
//how would I parse the BufferedImage to the InputStream below without saving the png first but straight from RAM
InputStream in = new InputStream();
int len;
while ((len = in.read(buffer)) > 0)
{
zipOutputStream.write(buffer, 0, len);
}
in.close();
zipOutputStream.closeEntry();
zipOutputStream.close();
}
Use ImageIO.write(RenderedImage im,String formatName, OutputStream output) to write the output. Pass a ZipOutputStream for the OutputStream parameter.
Check this page for more info.

Categories

Resources