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(o开发者_StackOverflowbuffer, 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();
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)
{
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.
精彩评论