Is the code that I use to make .zip files correct?

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(); 
+1
source share
2 answers

Perhaps the problem is with the mode of the FileInputStream object. This link (has C # code) states that the stream should be readable. Try changing your code according to their recommendations. Posting part of the code from your site:

 using (var raw = File.Open(inputFileName, FileMode.Open, FileAccess.Read)) { using (var input= new ZipInputStream(raw)) { ZipEntry e; while (( e = input.GetNextEntry()) != null) { 
0
source

Considering what we had on this issue , the size of your record is set to 4294967295, which is the reason that you have a problem with unpacking in java. Try setting the size:

 FileInfo fi = new FileInfo(Fil); // added this line here oZipEntry = new ZipEntry(Fil.Remove(0, TrimLength)); oZipEntry.Size = fi.Length; // added this line here oZipStream.PutNextEntry(oZipEntry); 

Sorry if the syntax is incorrect, this is not verified.

0
source

All Articles