我用来制作.zip文件的代码是否正确?
问题描述:
我使用C#这个代码的zip文件。我需要打开Android应用中这些文件(JAVA):我用来制作.zip文件的代码是否正确?
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();
我有在Java中提取这些文件的问题,我想确保问题不是来自zip文件文件..所以这段代码是否正确? Java可以读取这些zip文件吗?
我试着建立正常使用WinRAR和文件提取码给出了同样的问题..问题是,“zin.getNextEntry()”总是空:
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();
答
你的问题可能是由于到FileInputStream对象的模式。 This link (has C# code)指出流必须可读。尝试根据他们的建议更改您的代码。从他们的网站上发布的部分代码:
using (var raw = File.Open(inputFileName, FileMode.Open, FileAccess.Read))
{
using (var input= new ZipInputStream(raw))
{
ZipEntry e;
while ((e = input.GetNextEntry()) != null)
{
+0
此代码解压缩文件..我想压缩在C#中,并解压缩在Java中.. – Omar
答
从dicussion我们对this question,您的条目的大小被设置为4294967295,这是您遇到的解压缩问题的原因在Java中。尝试设置大小:
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);
道歉,如果语法不正确,这是未经测试的。
为什么不手动创建一个zip文件并用它测试java提取。如果那有效,那么你的创作可能是错误的。 –
您是否尝试过在WinZip或7zip中打开zip文件?它工作吗? –
在C#中,你应该在你的一次性物品周围使用''use'块。 – Aren