是code我使用,使.zip文件是否正确? [英] Is the code I'm using to make .zip files correct?
问题描述
我使用这个code在C#中的zip文件。我需要打开在Android应用这些文件(JAVA):
字符串mp3Files =E:\\\\;
INT TrimLength = mp3Files.ToString()长度。字节[] obuffer;
字符串outpath中= mp3Files +\\\\+ I +.ZIP
ZipOutputStream oZipStream =新ZipOutputStream(File.Create(outpath中)); //创建zip流
oZipStream.SetLevel(9); //最大COM pression的foreach(字符串费尔在AR)//为每个文件,生成的ZipEntry
{ oZipEntry =新的ZipEntry(Fil.Remove(0,TrimLength));
oZipStream.PutNextEntry(oZipEntry); 如果(!Fil.EndsWith(@/))//如果文件结尾'/'的目录
{
ostream的= File.OpenRead(费尔);
obuffer =新的字节[ostream.Length]
ostream.Read(obuffer,0,obuffer.Length);
oZipStream.Write(obuffer,0,obuffer.Length);
}
}
oZipStream.Finish();
oZipStream.Close();
我在提取中的java中的这些文件的问题,我想从zip文件的文件以确保问题心不是..所以这是code正确吗?能够在Java阅读这些拉链?
我只是试图建立正常用winrar和文件提取code给出了同样的问题..问题是,zin.getNextEntry()总是空:
字符串的压缩文件=路径+文件名;
的FileInputStream鳍=新的FileInputStream(压缩文件);
ZipInputStream ZIN =新ZipInputStream(翅); ZipEntry的泽= NULL;
而((ZE = zin.getNextEntry())!= NULL){
UnzipCounter ++;
如果(ze.isDirectory()){
dirChecker(ze.getName());
}其他{
FileOutputStream中FOUT =新的FileOutputStream(路径
+ ze.getName());
而((Unziplength = zin.read(Unzipbuffer))大于0){
fout.write(Unzipbuffer,0,Unziplength);
}
zin.closeEntry();
fout.close(); } }
zin.close();
从dicussion我们对<一个href=\"http://stackoverflow.com/questions/7561031/zipinputstream-getnextentry-is-null-when-extracting-zip-files\">this问题,您的条目的大小被设置为4294967295,这是你遇到与Java中的解压缩问题的原因。尝试设置大小:
FileInfo的网络连接=新的FileInfo(费尔); //加入这一行这里
oZipEntry =新的ZipEntry(Fil.Remove(0,TrimLength));
oZipEntry.Size = fi.Length; //加入这一行这里
oZipStream.PutNextEntry(oZipEntry);
道歉,如果语法不正确,这是未经考验的。
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();
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.
这篇关于是code我使用,使.zip文件是否正确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!