I use this code in C # for zip files. I need to open these files in an android application (java):
String mp3Files = "E:\\"; int TrimLength = mp3Files.ToString().Length; byte[] obuffer; string outPath = mp3Files + "\\" + i + ".zip"; ZipOutputStream oZipStream = new ZipOutputStream(File.Create(outPath)); // create zip stream oZipStream.SetLevel(9); // maximum compression foreach (string Fil in ar) // for each file, generate a zipentry { oZipEntry = new ZipEntry(Fil.Remove(0, TrimLength)); oZipStream.PutNextEntry(oZipEntry); if (!Fil.EndsWith(@"/")) // if a file ends with '/' its a directory { ostream = File.OpenRead(Fil); obuffer = new byte[ostream.Length]; ostream.Read(obuffer, 0, obuffer.Length); oZipStream.Write(obuffer, 0, obuffer.Length); } } oZipStream.Finish(); oZipStream.Close();
I am having problems extracting these files in java and I want to make sure that the problem is not in the zip files .. is this code correct? Can java read these zippers?
I was just trying to create normally using winrar, and the file extraction code gives the same problem. The problem is that "zin.getNextEntry ()" is always null:
String zipFile = Path + FileName; FileInputStream fin = new FileInputStream(zipFile); ZipInputStream zin = new ZipInputStream(fin); ZipEntry ze = null; while ((ze = zin.getNextEntry()) != null) { UnzipCounter++; if (ze.isDirectory()) { dirChecker(ze.getName()); } else { FileOutputStream fout = new FileOutputStream(Path + ze.getName()); while ((Unziplength = zin.read(Unzipbuffer)) > 0) { fout.write(Unzipbuffer, 0, Unziplength); } zin.closeEntry(); fout.close(); } } zin.close();
source share