Converting ZipOutputStream to ZipInputStream - java

I am creating a ZipOutputStream and adding files to it this is being done in memory , later I want to be able to get/read files from this. I have tried multiple permutations and combinations but am not able to get it done.
I know if I use FileOutpuStream and attach a physical file to it I will be able to use it but I am not allowed to create physical file.
Current Code:-
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ZipOutputStream zos = new ZipOutputStream(bos);
ZipEntry ze = new ZipEntry(“Temp.txt”);
zos.putNextEntry(ze);
FileInputStream fis = new FileInputStream("Test/File1.txt");
byte[] buffer = new byte[1024];
int len;
while ((len = fis.read(buffer)) > 0) {
//System.out.println(new String(buffer));
zos.write(buffer, 0, len);
}

You're on the right track! To read the file back, simply use:
byte[] zipBytes = bos.toByteArray();
ByteArrayInputStream bis = new ByteArrayInputStream(zipBytes);
ZipInputStream zis = new ZipInputStream(bis);

Related

unable to create zip file in java useing java.util.zip, getting archive is corrupt

I am able to create the zip folder with some pdf. but when I am unziping it is getting archive is corrupt.
here is the code I tried.
//create byte buffer
byte[] buffer = new byte[1024];
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ZipOutputStream zout = new ZipOutputStream(bos);
zout.putNextEntry(new ZipEntry("first.pdf"));
int length;
//con.getInputStream() is the inputstream
while((length = con.getInputStream().read(buffer)) > 0)
{
zout.write(buffer, 0, length);
}
zout.closeEntry();
resp.setContentType(CONTENT_TYPE_ZIP);
resp.setHeader("Content-Disposition", "inline;filename=first.zip");
resp.getOutputStream().write(bos.toByteArray());
pdfMsOfficeCacheHeaderUtil.process(request, resp);
resp.flushBuffer();
zout.flush();
zout.finish();
zout.close();
bos.close();
please let me know where I am doing wrong.
You're flushing, finishing and closing your zout after you've written the contents of bos to the response stream.
First finish writing the zip data, then write the data to the response so it'll be correct.
I got the solution.
//create byte buffer
byte[] buffer = new byte[1024];
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ZipOutputStream zout = new ZipOutputStream(bos);
zout.putNextEntry(new ZipEntry("first.pdf"));
int length;
while((length = content.getInputStream().read(buffer)) > 0)
{
zout.write(buffer, 0, length);
}
zout.flush();
zout.finish();
bos.flush();
resp.setContentType(CONTENT_TYPE_ZIP);
resp.setHeader("Content-Disposition", "inline;filename= first.zip");
resp.getOutputStream().write(bos.toByteArray());
pdfMsOfficeCacheHeaderUtil.process(request, resp);
resp.flushBuffer();
zout.close();
bos.close();
It worked for me.

Convert file to byte array in reverse order

byte[] bFile = new byte[(int) file.length()];
FileInputStream fileInputStream = new FileInputStream(file);
fileInputStream.read(bFile);
fileInputStream.close();
This code let's me convert file to byte array, am looking for reading file from end to start (in reverse order)
Edit : i dont wan't to read entire file. A part at the end (Example around 1000 bytes)
File file = new File(/*file path*/);
byte[] bFile = new byte[1000];
RandomAccessFile fileInputStream = new RandomAccessFile(file, "r");
fileInputStream.seek(fileInputStream.length() - bFile[0].length);
fileInputStream.read(bFile, 0, bFile.length);
fileInputStream.close();
I just figured it out, reading last 1000 bytes of a file

Incorrect upload file use FileOutputStream. Incorrect size

When I try upload the file from server, I use this code. I get the file size of 500kb, when the original file size about 300kb.
What am I doing wrong?
attachmentContent = applicationApi.getApplicationAttachmentContent(applicationame);
InputStream in = new BufferedInputStream(attachmentContent.getAttachmentContent().getInputStream());
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int n=0;
while (-1 !=(n=in.read(buf)))
{
out.write(buf,0,n);
}
out.close();
in.close();
byte[] response = out.toByteArray();
File transferredFile = new File(attachmentName);
FileOutputStream outStream = new FileOutputStream(transferredFile);
attachmentContent.getAttachmentContent().writeTo(outStream);
outStream.write(response);
outStream.close();
Simplify. The same result:
File transferredFile = new File(attachmentName);
FileOutputStream outStream = new FileOutputStream(transferredFile);
attachmentContent.getAttachmentContent().writeTo(outStream);
outStream.close();
The ByteArrayOutputStream is a complete waste of time and space. You're reading the attachment twice, and writing it twice too. Just read from the attachment and write directly to the file. Simplify, simplify. You don't need 90% of this.

java download file does not work in file size is more than 8K?

i try to download a pdf file from url using the code below. It works fine when the file size is under 8k and if the file is up to 8k, it downloads the pdf file but the file is not readable.
Code
InputStream in = new BufferedInputStream(url.openStream());
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
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(new File("C:\\temp\\TEST.pdf"));
fos.write(response);
fos.close();
System.out.println("Download Finished");
You may try using the IOUtils.copy(InputStream input, OutputStream output)
You can minimize the lines of code.
Apache IOUtils

java server/client save a downloadad file NOT to HDD

Im receiving a file trough this code and the "bos.write" are saving it o to my HDD.
Everything working good.
Since im sending the file in a few second i thought i could store the file in memory
instead of HDD.
Now how do i do this?
File path = new File("C://anabella//test1.txt");
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(path));
int size = 1024;
int val = 0;
byte[] buffer = new byte[1024];
while (fileSize >0) {
val = in.read(buffer, 0, size);
bos.write(buffer, 0, val);
fileSize -= val;
if (fileSize < size)
size = (int) fileSize;
}
Presumably bos is a FileOutputStream? To use an in-memory buffer use a ByteArrayOutputStream instead.
If you know the size in advance you don't even need a ByteArrayOutputStream
InputStream is = socket.getInputStream(); // or where ever the inputstream comes from.
DataInputStream in = new DataInputStream(is);
byte[] bytes = new byte[fileSize];
in.readFully(bytes);
to send the bytes to any OutputStream like
OutputStream os = ...
os.write(bytes);
The bytes will contain the contents of the file.

Categories