Java: unzipping is significantly slower than zipping - java

I have some code that zips up a file sends it over the network and then unzips it on the other end. I'm still testing the code and the source and the destination are the same. Zipping up the file takes on the order of a minute. Unzipping the file takes on the order of an hour. I'm think there must be a flaw in my code to have such a large difference. Here's the code to unzip:
public String uncompressLocalZip(String filename,String strUUID,ParentEntry pe,boolean bControlFileProgress) {
final int BUFFER = 2048;
BufferedOutputStream out = null;
ZipInputStream zis = null;
try {
FileInputStream fis = new FileInputStream(Constants.conf.getFileDirectory() + Constants.PATH_SEPARATOR + strUUID + Constants.PATH_SEPARATOR + filename);
zis = new ZipInputStream(new BufferedInputStream(fis));
ZipEntry entry;
long totallength = 0;
long size = 0;
if (pe !=null)
size = pe.getSize();
while((entry = zis.getNextEntry()) != null) {
System.out.println("Extracting: " +entry);
int count;
byte data[] = new byte[BUFFER];
// write the files to the disk
File fileOutput = new File(Constants.conf.getFileDirectory() + Constants.PATH_SEPARATOR + strUUID + Constants.PATH_SEPARATOR + Constants.conf.getUncompressFolderName() + Constants.PATH_SEPARATOR + entry.getName());
new File(fileOutput.getParent()).mkdirs();
BufferedOutputStream fos = new BufferedOutputStream(new FileOutputStream(fileOutput));
out = new BufferedOutputStream(fos, BUFFER);
while ((count = zis.read(data, 0, BUFFER)) != -1) {
out.write(data, 0, count);
totallength += count;
}
out.flush();
}
}
catch(Exception e) {
e.printStackTrace();
return("FAILED");
}
finally {
try {if ( out!= null) out.close();} catch (IOException ioe) {}
try {if ( zis!= null) zis.close();} catch (IOException ioe) {}
}
return("SUCCESS");
}
Here's the code to zip:
public void createLocalZip(String filename,ProcessEntry pe) {
ZipOutputStream out=null;
try {
File fileOutput = new File (filename);
out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(fileOutput)));
long totallength=0;
long size = pe.getParentEntry().getSize();
String strStartDirectory;
if (pe.getParentEntry().isDirectory())
strStartDirectory=pe.getParentEntry().getUrl();
else
strStartDirectory=pe.getParentEntry().getFolder();
for (int i=0;i<pe.getParentEntry().tableModel3.getRowCount();i++) {
FileEntry fe = pe.getParentEntry().tableModel3.getFileEntry(i);
File fileInput = new File (fe.getUrl());
FileInputStream input = new FileInputStream(fileInput);
BufferedInputStream in = new BufferedInputStream(input);
String strRelativeDir = fe.getUrl().substring(strStartDirectory.length()+1,fe.getUrl().length());
ZipEntry entry = new ZipEntry(strRelativeDir);
out.putNextEntry(entry);
byte[] bbuf = new byte[2048];
int length=0;
while ((in != null) && ((length = in.read(bbuf)) != -1)) {
out.write(bbuf,0,length);
totallength += length;
pe.setProgress((int) (totallength*100/size));
}
in.close();
}
}
catch (Exception e) {
System.out.println(e.getMessage());
}
finally {
try {if (out!=null) out.close();} catch(IOException ioe){}
}
}
Update: The compression ratio for this particular test is about 90% (1.2GB down to about 100MB). So I suppose it could be the extra writing to disk for unzipping vs. zipping, although I would expect close to a 10X differential vs 60X.

don't double wrap your OutputStream with BufferedOutputStream (you only need 1 BufferedOutputStream wrapper), and close it after you are done writing to it.
also, ZipEntrys can be directories, so check that and handle accordingly.

I have no really big file to test your code, so I can only guess.
You say your uncompressed zip size is more than 1 GB. This could be more than fits in your memory, and if something forces the VM to fit everything in memory, it will have to swap. Observe your program with a profiler.
Make sure your close each FileOutputStream after writing to it. (You create lots of them, and only close the last one.)
I'm not sure about the ZipInputStream implementation (maybe it forces your BufferedStream to buffer much of data). You could try ZipFile instead (which allows random access, basically).

Consider using a specialized library to do the zipping/unzipping. http://sevenzipjbind.sourceforge.net/ might help.

Related

File md5 hash changes when chunking it (for netty transfer)

Question at the bottom
I'm using netty to transfer a file to another server.
I limit my file-chunks to 1024*64 bytes (64KB) because of the WebSocket protocol. The following method is a local example what will happen to the file:
public static void rechunck(File file1, File file2) {
FileInputStream is = null;
FileOutputStream os = null;
try {
byte[] buf = new byte[1024*64];
is = new FileInputStream(file1);
os = new FileOutputStream(file2);
while(is.read(buf) > 0) {
os.write(buf);
}
} catch (IOException e) {
Controller.handleException(Thread.currentThread(), e);
} finally {
try {
if(is != null && os != null) {
is.close();
os.close();
}
} catch (IOException e) {
Controller.handleException(Thread.currentThread(), e);
}
}
}
The file is loaded by the InputStream into a ByteBuffer and directly written to the OutputStream.
The content of the file cannot change while this process.
To get the md5-hashes of the file I've wrote the following method:
public static String checksum(File file) {
InputStream is = null;
try {
is = new FileInputStream(file);
MessageDigest digest = MessageDigest.getInstance("MD5");
byte[] buffer = new byte[8192];
int read = 0;
while((read = is.read(buffer)) > 0) {
digest.update(buffer, 0, read);
}
return new BigInteger(1, digest.digest()).toString(16);
} catch(IOException | NoSuchAlgorithmException e) {
Controller.handleException(Thread.currentThread(), e);
} finally {
try {
is.close();
} catch(IOException e) {
Controller.handleException(Thread.currentThread(), e);
}
}
return null;
}
So: just in theory it should return the same hash, shouldn't it? The problem is that it returns two different hashes that do not differ with every run.. file size stays the same and the content either.
When I run the method once for in: file-1, out: file-2 and again with in: file-2 and out: file-3 the hashes of file-2 and file-3 are the same! This means the method will properly change the file every time the same way.
1. 58a4a9fbe349a9e0af172f9cf3e6050a
2. 7b3f343fa1b8c4e1160add4c48322373
3. 7b3f343fa1b8c4e1160add4c48322373
Here is a little test that compares all buffers if they are equivalent. Test is positive. So there aren't any differences.
File file1 = new File("controller/templates/Example.zip");
File file2 = new File("controller/templates2/Example.zip");
try {
byte[] buf1 = new byte[1024*64];
byte[] buf2 = new byte[1024*64];
FileInputStream is1 = new FileInputStream(file1);
FileInputStream is2 = new FileInputStream(file2);
boolean run = true;
while(run) {
int read1 = is1.read(buf1), read2 = is2.read(buf2);
String result1 = Arrays.toString(buf1), result2 = Arrays.toString(buf2);
boolean test = result1.equals(result2);
System.out.println("1: " + result1);
System.out.println("2: " + result2);
System.out.println("--- TEST RESULT: " + test + " ----------------------------------------------------");
if(!(read1 > 0 && read2 > 0) || !test) run = false;
}
} catch (IOException e) {
e.printStackTrace();
}
Question: Can you help me chunking the file without changing the hash?
while(is.read(buf) > 0) {
os.write(buf);
}
The read() method with the array argument will return the number of files read from the stream. When the file doesn't end exactly as a multiple of the byte array length, this return value will be smaller than the byte array length because you reached the file end.
However your os.write(buf); call will write the whole byte array to the stream, including the remaining bytes after the file end. This means the written file gets bigger in the end, therefore the hash changed.
Interestingly you didn't make the mistake when you updated the message digest:
while((read = is.read(buffer)) > 0) {
digest.update(buffer, 0, read);
}
You just have to do the same when you "rechunk" your files.
Your rechunk method has a bug in it. Since you have a fixed buffer in there, your file is split into ByteArray-parts. but the last part of the file can be smaller than the buffer, which is why you write too many bytes in the new file. and that's why you do not have the same checksum anymore. the error can be fixed like this:
public static void rechunck(File file1, File file2) {
FileInputStream is = null;
FileOutputStream os = null;
try {
byte[] buf = new byte[1024*64];
is = new FileInputStream(file1);
os = new FileOutputStream(file2);
int length;
while((length = is.read(buf)) > 0) {
os.write(buf, 0, length);
}
} catch (IOException e) {
Controller.handleException(Thread.currentThread(), e);
} finally {
try {
if(is != null)
is.close();
if(os != null)
os.close();
} catch (IOException e) {
Controller.handleException(Thread.currentThread(), e);
}
}
}
Due to the length variable, the write method knows that until byte x of the byte array, only the file is off, then there are still old bytes in it that no longer belong to the file.

How to copy content from a zip to an Android directory?

Hello everyone.
Ask this question so that they could give me a hand and guide me on my way.
My problem
I want to be able to unzip a zip into a folder or directory on the SD card, but my code has not reached the target. Its error is that it does not decompress or copy any of the files in the destination directory. The zip is located in the resources resource folder.
My code
private boolean copyFile1(String filename1, String outPath1) {
AssetManager assetManager = this.getAssets();
final int CHUNK_SIZE = 1024 * 4;
InputStream in;
OutputStream out;
try {
in = assetManager.open(filename1);
String newFileName = outPath1;
ZipInputStream zipStream = new ZipInputStream(in);
ZipEntry zEntry = null;
while ((zEntry = zipStream.getNextEntry()) != null) {
if (zEntry.isDirectory()) {
} else {
FileOutputStream fout = new FileOutputStream(new File(outPath1));
BufferedOutputStream bufout = new BufferedOutputStream(fout);
byte[] buffer = new byte[CHUNK_SIZE];
int read = 0;
while ((read = zipStream.read(buffer)) != -1) {
bufout.write(buffer, 0, read);
}
zipStream.closeEntry();
bufout.close();
fout.close();
}
}
zipStream.close();
Log.d("Unzip", "Unzipping complete. path : " );
} catch (Exception e) {
Log.e("TAG", e.getMessage());
}
return true;
}
If they realize where I failed in my code or I know otherwise. Please let me know. Thank you

FileInputStream for a raw file

I have a raw file in res/raw named "pack.dat".
I can open an InputStream with the following code:
InputStream fis = null;
try {
fis = context.getResources().openRawResource(R.raw.pack);
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
String nextLine;
int i = 0, j = 0;
while ((nextLine = br.readLine()) != null) {
if (j == 5) {
j = 0;
i++;
}
}
} catch (Exception e) {
e.printStackTrace();
}
This is working, I can read from that file.
But unfortunately I need a FileInputStream. When I do this:
FileInputStream fs = null;
Uri url = Uri.parse("android.resource://" +
context.getPackageName() + "/" + R.raw.pack);
File file = new File(url.toString());
try {
fs = new FileInputStream(file);
fs.getChannel().position(0);
fs.read(bDatensatz, 0, indexlaenge);
} catch (Exception e) {
e.printStackTrace();
}
I get a "file not found" at
fs = new FileInputStream(file);
context.getResources().openRawResource(R.raw.pack) in the first example returns an InputStream.
What can I use to get a FileInputStream instead?
copied from another thread! may be this should help you
FileInputStream fis;
fis = openFileInput("test.txt");
StringBuffer fileContent = new StringBuffer("");
byte[] buffer = new byte[1024];
int n = 0;
while ((n = fis.read(buffer)) != -1)
{
fileContent.append(new String(buffer, 0, n));
}
But unfortunately I need a FileInputStream
Why?
I get a "file not found" at fs = new FileInputStream(file);
That is because you are trying to open something that is not a file.
What can I use to get a FileInputStream instead?
You would need to copy the resource to a local file (e.g., using openFileOutput() and Java I/O). Then, you can open the local file (e.g., using openFileInput()) and get a FileInputStream.
Or, just use the InputStream, fixing whatever code that you are using that is expecting a FileInputStream.

Reading JPEG Stream over socket gives Null characters

I am reading a .jpg file over InputStream using this code but I am receiving NULNUL...n stream after some text. Ii am reading this file link to file and link of file that I received , link is Written File link.
while ((ret = input.read(imageCharArray)) != -1) {
packet.append(new String(imageCharArray, 0, ret));
totRead += ret;
imageCharArray = new char[4096];
}
file = new File(
Environment.getExternalStorageDirectory()
+ "/FileName_/"
+ m_httpParser.filename + ".jpg");
PrintWriter printWriter = new PrintWriter(file);
// outputStream = new FileOutputStream(file); //also Used FileoutputStream for writting
// outputStream.write(packet.toString().getBytes());//
// ,
printWriter.write(packet.toString());
// outputStream.close();
printWriter.close();
}
I have also tried FileoutputStream but hardlucj for this too as commented in my code.
Edit
I have used this also. I have a content length field upto which i am reading and writing
byte[] bytes = new byte[1024];
int totalReadLength = 0;
// read untill we have bytes
while ((read = inputStream.read(bytes)) != -1
&& contentLength >= (totalReadLength)) {
outputStream.write(bytes, 0, read);
totalReadLength += read;
System.out.println(" read size ======= "
+ read + " totalReadLength = "
+ totalReadLength);
}
String is not a container for binary data, and PrintWriter isn't a way to write it. Get rid of all, all, the conversions between bytes and String and vice versa, and just transfer the bytes with input and output streams:
while ((count = in.read(buffer)) > 0)
{
out.write(buffer, 0, count);
}
If you need to constrain the number of bytes read from the input, you have to do that before calling read(), and you also have to constrain the read() correctly:
while (total < length && (count = in.read(buffer, 0, length-total > buffer.length ? buffer.length: (int)(length-total))) > 0)
{
total += count;
out.write(buffer, 0, count);
}
I tested it in my Nexus4 and it's working for me. Here is the snippet of code what I tried :
public void saveImage(String urlPath)throws Exception{
String fileName = "kumar.jpg";
File folder = new File("/sdcard/MyImages/");
// have the object build the directory structure, if needed.
folder.mkdirs();
final File output = new File(folder,
fileName);
if (output.exists()) {
output.delete();
}
InputStream stream = null;
FileOutputStream fos = null;
try {
URL url = new URL(urlPath);
stream = url.openConnection().getInputStream();
// InputStreamReader reader = new InputStreamReader(stream);
DataInputStream dis = new DataInputStream(url.openConnection().getInputStream());
byte[] fileData = new byte[url.openConnection().getContentLength()];
for (int x = 0; x < fileData.length; x++) { // fill byte array with bytes from the data input stream
fileData[x] = dis.readByte();
}
dis.close();
fos = new FileOutputStream(output.getPath());
fos.write(fileData);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (stream != null) {
try {
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Just Call the above function in a background thread and pass your url. It'll work for sure. Let me know if it helps.
You can check below code.
destinationFile = new File(
Environment.getExternalStorageDirectory()
+ "/FileName_/"
+ m_httpParser.filename + ".jpg");
BufferedOutputStream buffer = new BufferedOutputStream(new FileOutputStream(destinationFile));
byte byt[] = new byte[1024];
int i;
for (long l = 0L; (i = input.read(byt)) != -1; l += i ) {
buffer.write(byt, 0, i);
}
buffer.close();

Joining two mp3 files into one

I have this code to read bytes to another file.
But I'm not able to concatenate two mp3 files into one.
Am I missing something?
public static void main(String[] args) {
String strFileName = ("D:/Music/Assb/Love.mp3");
BufferedOutputStream bos = null;
try
{
//create an object of FileOutputStream
FileOutputStream fos = new FileOutputStream(new File(strFileName));
//create an object of BufferedOutputStream
bos = new BufferedOutputStream(fos);
String str = "D:/Music/Assembled/Heart001.mp3"
+ "D:/Music/Assembled/Heart002.mp3";
/*
* To write byte array to file use,
* public void write(byte[] b) method of BufferedOutputStream
* class.
*/
System.out.println("Writing byte array to file");
bos.write(str.getBytes());
System.out.println("File written");
It`s suck. Mp3 file starts with headers. For correct merging you have to skip first 32 bytes. Try this.
try {
FileInputStream fistream1 = new FileInputStream(_file_name);
File f = new File(new File(_file_name).getParent()+"/final.mp3");
if(!f.exists())
{
f.createNewFile();
}
FileOutputStream sistream = new FileOutputStream((new File(_file_name)).getParent()+"/final.mp3");
int temp;
int size = 0;
temp = fistream1.read();
while( temp != -1)
{
sistream.write(temp);
temp = fistream1.read();
};
fistream1.close();
FileInputStream fistream2 = new FileInputStream(temp_file);
fistream2.read(new byte[32],0,32);
temp = fistream2.read();
while( temp != -1)
{
sistream.write(temp);
temp = fistream2.read();
};
fistream2.close();
sistream.close();
} catch (IOException e) {
e.printStackTrace();
}
You need to do this in two steps
String str = "D:/Music/Assembled/Heart001.mp3";
>>> ADD code to open the file given by str <<<<
bos.write(strFile.getBytes());
>>> Add code to close the file
str = "D:/Music/Assembled/Heart002.mp3";
>>> ADD code to open the file given by str <<<<
bos.write(strFile.getBytes());
>>> Add code to close the file
And as you can see you need code to open the mp3 file to read it
What Are You Trying For...Actually..if You Want To Read 2 Files to Byte Stream the dont String str = "D:/Music/Assembled/Heart001.mp3"
+ "D:/Music/Assembled/Heart002.mp3";
make str1=D:/Music/Assembled/Heart001.mp3 and str2=D:/Music/Assembled/Heart002.mp3 and read str1,str2 seperately through bufferedoutputsream
This code will work well and merge audio of similar type with in seconds...
try {
InputStream in = new FileInputStream("C:\\a.mp3");//firstmp3
byte[] buffer = new byte[1 << 20]; // loads 1 MB of the file
OutputStream os = new FileOutputStream(new File("C:\\output.mp3", true);//output mp3
int count;
while ((count = in.read(buffer)) != -1) {
os.write(buffer, 0, count);
os.flush();
}
in.close();
in = new FileInputStream("C:\\b.mp3");//second mp3
while ((count = in.read(buffer)) != -1) {
os.write(buffer, 0, count);
os.flush();
}
in.close();
os.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

Categories

Resources