I've been working on a function that compresses an array of bytes using GZIP and sends it through an outputStream that belongs to a socket. It downloads fine but when trying to decompress on my PC it says that file is corrupted.
private void Zip(byte[] datatocompress)
{
ZipOutputStream zippoutstream = new ZipOutputStream(outputstream);
zippoutstream.putNextEntry(new ZipEntry("file.html"));
zippoutstream.write(datatocompress);
zippoutstream.closeEntry();
zippoutstream.flush();
zippoutstream.close();
}
No idea about what crashes. Any suggestion?
public static byte[] gzip(byte[] val) throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream(val.length);
GZIPOutputStream gos = null;
try {
gos = new GZIPOutputStream(bos);
gos.write(val, 0, val.length);
gos.finish();
gos.flush();
bos.flush();
val = bos.toByteArray();
} finally {
if (gos != null)
gos.close();
if (bos != null)
bos.close();
}
return val;
}
/**
* Compress
*
* #param source
*
* #param target
*
* #throws IOException
*/
public static void zipFile(String source, String target) throws IOException {
FileInputStream fin = null;
FileOutputStream fout = null;
GZIPOutputStream gzout = null;
try {
fin = new FileInputStream(source);
fout = new FileOutputStream(target);
gzout = new GZIPOutputStream(fout);
byte[] buf = new byte[1024];
int num;
while ((num = fin.read(buf)) != -1) {
gzout.write(buf, 0, num);
}
} finally {
if (gzout != null)
gzout.close();
if (fout != null)
fout.close();
if (fin != null)
fin.close();
}
}
Related
I have the following dowload method that takes an URL and a File. It attempts to resume the file, but if the file is already completed it returns error 416 - Range Not Satisfiable.
How to detect the file is complete?
private void download(final URL source, final File file) throws IOException {
URLConnection connection = source.openConnection();
connection.setConnectTimeout(30);
connection.setReadTimeout(30);
FileOutputStream fos = null;
try {
if (file.exists()) {
fos = new FileOutputStream(file, true);
connection.setRequestProperty("Range", "bytes=" + file.length() + "-");
} else {
fos = new FileOutputStream(file);
}
try (InputStream in = connection.getInputStream()) {
try (BufferedOutputStream bout = new BufferedOutputStream(fos, 1024)) {
byte[] data = new byte[1024];
int x = 0;
while ((x = in.read(data, 0, 1024)) >= 0) {
bout.write(data, 0, x);
}
}
} catch (IOException ioe) {
if (connection instanceof HttpURLConnection) {
var httpUrlConn = (HttpURLConnection) connection;
int response = httpUrlConn.getResponseCode();
if (response == 416) {
}
}
}
} finally {
if (fos != null) {
fos.close();
}
}
}
I am zipping the files using , but while unzipping them, I am facing two problems,
when unzipped without buffer its getting back to original form, but when I use buffer its not able to do it correctly.
the size of the unzipped file is more than the original file.
private static void writeFile(FileOutputStream fos, String zipFilePath) throws IOException {
try (FileInputStream fis = new FileInputStream(zipFilePath);
GZIPInputStream inflaterInputStream = new GZIPInputStream(fis)) {
int data;
**while ((data = inflaterInputStream.read()) != -1) {//without buffer**
fos.write(data);
}
}
}
private static void writeFile(FileOutputStream fos, String zipFilePath) throws IOException {
byte[] buffer = new byte[12048];
try (FileInputStream fis = new FileInputStream(zipFilePath);
GZIPInputStream inflaterInputStream = new GZIPInputStream(fis)) {
int data;
**while ((data = inflaterInputStream.read(buffer)) != -1) {//with buffer**
fos.write(data);
}
}
}
You're not writing the buffer, but data which is the length of bytes read...
Corrected:
private static void writeFile(FileOutputStream fos, String zipFilePath) throws IOException {
byte[] buffer = new byte[12048];
try (InputStream fis = new FileInputStream(zipFilePath);
InputStream inflaterInputStream = new GZIPInputStream(fis)) {
int data;
while ((data = inflaterInputStream.read(buffer)) != -1) {//with buffer**
fos.write(buffer, 0, data);
}
}
}
You'd be better off using apache.commons-io
private static void writeFile(FileOutputStream fos, String zipFilePath) throws IOException {
try (InputStream fis = new FileInputStream(zipFilePath);
InputStream inflaterInputStream = new GZIPInputStream(fis)) {
IOUtils.copy(fis, fos);
}
}
I've already seen
Is it possible to check progress of URLconnection.getInputStream()?
https://stackoverflow.com/a/20120451/5437621
I'm using the following code to download a file from internet:
try {
InputStream is = new URL(pdfUrl).openStream();
byte[] pdfData = readBytes(is);
return pdfData;
} catch (IOException e) {
e.printStackTrace();
return null;
}
public byte[] readBytes(InputStream inputStream) throws IOException {
ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
int len = 0;
while ((len = inputStream.read(buffer)) != -1) {
byteBuffer.write(buffer, 0, len);
}
return byteBuffer.toByteArray();
}
Is there any method I can get the progress of the file being downloaded ?
The answers I have seen are using a while loop but I don't understand how to use it in this case.
EDIT:
I'm using this in AsyncTask:
protected byte[] doInBackground(String... url) {
pdfUrl = url[0];
try {
InputStream is = new URL(pdfUrl).openStream();
DownloadBytes downloadData = readBytes(is);
byte[] pdfData = downloadData.getBytes();
progress = downloadData.getProgress();
return pdfData;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
How can I adjust publishProgress() in this method ?
I have a problem with the com.itextpdf.text.pdf.codec.GifImage.
In the constructor GifImage(URL url) the inputstream is used twice but only once closed.
If the stream isn't closed the file (temporary image) can not be deleted.
public GifImage(URL url) throws IOException {
fromUrl = url;
InputStream is = null;
try {
is = url.openStream(); // first use
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int read = 0;
byte[] bytes = new byte[1024];
while ((read = is.read(bytes)) != -1) {
baos.write(bytes, 0, read);
}
is = new ByteArrayInputStream(baos.toByteArray()); // second use
baos.flush();
baos.close();
process(is);
}
finally {
if (is != null) {
is.close();
}
}
}
This should solve your problem:
In my homework
writePosting(FileChannel fc, PostingList posting)
It seems to use fc to write contents of a class postingList, where PostingList contain an integer and a List.
But I find fc could only write bytes?....I do not even understand, why we put FileChannel as a parameter rather than a inputStream?
Could it write directly an integter or string? thanks!
You can accomplish that using serialization.
Class PostingList:
public class PostingList<T extends Serializable> implements Serializable{
private static final long serialVersionUID = 6893022784519772456L;
private Integer number;
private List<T> list;
public PostingList(Integer number, List<T> list) {
super();
this.number = number;
this.list = list;
}
#Override
public String toString() {
return "PostingList [number=" + number + ", list=" + list + "]";
}
}
writePosting:
public static void writePosting(FileChannel fc, PostingList<Integer> posting)
throws IOException {
ByteArrayOutputStream baos = null;
ObjectOutputStream oos = null;
try {
baos = new ByteArrayOutputStream();
oos = new ObjectOutputStream(baos);
oos.writeObject(posting);
oos.flush();
byte[] postingBytes = baos.toByteArray();
System.out.println("Bytes read: " + postingBytes.length);
ByteBuffer buffer = ByteBuffer.allocate(postingBytes.length);
// prepare buffer to fill with data.
buffer.clear();
// write the bytes
buffer.put(postingBytes);
// prepare for writing
buffer.flip();
fc.write(buffer);
} finally {
if (oos != null) {
oos.close();
}
if (baos != null) {
baos.close();
}
}
}
And an extra method, readPosting :
#SuppressWarnings({ "rawtypes", "unchecked" })
private static PostingList<Integer> readPosting(FileChannel fc)
throws IOException, ClassNotFoundException {
// Better to set this to a higher number
byte[] barray = new byte[32];
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ByteBuffer buffer = ByteBuffer.allocate(32);
int bytesRead = 0;
while ((bytesRead = fc.read(buffer)) != -1) {
buffer.flip();
buffer.get(barray, 0, bytesRead);
bos.write(barray, 0, bytesRead);
buffer.clear();
// for testing
System.out.println(bytesRead);
}
// We write the bytes recovered to an object
byte[] bytes = bos.toByteArray();
ByteArrayInputStream bis = null;
ObjectInputStream ois = null;
Object obj = null;
try {
bis = new ByteArrayInputStream(bytes);
ois = new ObjectInputStream(bis);
obj = ois.readObject();
} finally {
if (bis != null) {
bis.close();
}
if (ois != null) {
ois.close();
}
}
return (PostingList) obj;
}
And a little test in the main method:
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < 5; i++) {
list.add(i);
}
PostingList<Integer> pl = new PostingList<Integer>(100, list);
File f = new File("out.dat");
RandomAccessFile raf = null;
FileChannel fc = null;
try {
raf = new RandomAccessFile(f, "rw");
fc = raf.getChannel();
writePosting(fc, pl);
fc.close();
raf.close();
raf = new RandomAccessFile(f, "r");
fc = raf.getChannel();
System.out.println(readPosting(fc));
fc.close();
raf.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
if (fc!=null) {
try {
fc.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
if (raf!=null) {
try {
raf.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
Basically, this is reading/writing serialized objects into a file, but with FileChannel instead of the classic way.
EDIT:
Output:
Bytes read: 301
32
32
32
32
32
32
32
32
32
13
PostingList [number=100, list=[0, 1, 2, 3, 4]]