1

I'm using this code in C# to zip files.. I need to open these files in an Android app (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'm having problems in extracting these files in java and I want to make sure the problem isnt from zip files files.. so is this code correct? Can java read these zips?

I just tried 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();
Omar
  • 7,835
  • 14
  • 62
  • 108
  • 4
    why not create a zip file manually and test the java extraction with that. if that works then your creation is probably wrong. – Sam Holder Sep 28 '11 at 17:34
  • 2
    Have you tried opening the zip file in WinZip or 7zip? Does it work? – Mark Byers Sep 28 '11 at 17:37
  • 1
    In C# you should use a `using` block around your disposables. – Aren Sep 28 '11 at 17:43
  • Can you post the errors, if you mean are getting errors, when you refer to having problems? – Arun Sep 28 '11 at 17:50
  • @SamHolder I just tried that, and the unzip code doesnt work even when creating the .zip files with WinRar.... Yep winRar opens the files normally .... I edited the question with the problem – Omar Sep 28 '11 at 18:10
  • Please also see the following: http://stackoverflow.com/questions/7589609/unzip-process-works-on-one-zip-while-it-doesnt-on-another & http://stackoverflow.com/questions/7561031/zipinputstream-getnextentry-is-null-when-extracting-zip-files – Matthew Farwell Sep 28 '11 at 21:36

2 Answers2

0

Your problem might be due to FileInputStream object's mode. This link (has C# code) states the stream must be readable. Try changing your code according to their recommendation. Posting part of the code from their site:

using (var raw = File.Open(inputFileName, FileMode.Open, FileAccess.Read))
{
    using (var input= new ZipInputStream(raw))
    {
        ZipEntry e;
        while (( e = input.GetNextEntry()) != null)
        {
Arun
  • 2,493
  • 15
  • 12
0

From the dicussion we've had on this question, the size of your entry is being set to 4294967295, which is the reason you're having a problem with the unzip 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);

Apologies if the syntax is incorrect, this is untested.

Community
  • 1
  • 1
Matthew Farwell
  • 60,889
  • 18
  • 128
  • 171
  • I used that but I get the error: "[ICSharpCode.SharpZipLib.Zip.ZipException] = {"size was 85985, but I expected 32"}" – Omar Sep 29 '11 at 08:48
  • Edited: you need to use the size of the file, not the size of the file name. Apologies. – Matthew Farwell Sep 29 '11 at 08:57
  • I put your new code and there is a small difference between before and now in the .zip size.. I'm having very weird results.. some times it unzips normally, sometime it doesnt, and sometimes it unzips parts of the zip file :( .... I just tried to lower the level of the compression (oZipStream.SetLevel(4);) and now it unzips – Omar Sep 29 '11 at 11:42