2010-06-04 44 views
2

每當我嘗試獲取文件時,輸入流(s.Length)的長度始終爲零,我做錯了什麼?的ZipEntry是有效的並且具有的文件的正確的大小等sharpziplib +提取單個文件

下面是使用代碼IM:

public static byte[] GetFileFromZip(string zipPath, string fileName) 
{ 
    byte[] ret = null; 
    ZipFile zf = new ZipFile(zipPath); 
    ZipEntry ze = zf.GetEntry(fileName); 

    if (ze != null) 
    { 
     Stream s = zf.GetInputStream(ze); 
     ret = new byte[s.Length]; 
     s.Read(ret, 0, ret.Length); 
    } 

    return ret; 
} 

回答

9

輸入流將不具有的長度。改爲使用ZipEntry.Size

public static byte[] GetFileFromZip(string zipPath, string fileName) 
{ 
    byte[] ret = null; 
    ZipFile zf = new ZipFile(zipPath); 
    ZipEntry ze = zf.GetEntry(fileName); 

    if (ze != null) 
    { 
     Stream s = zf.GetInputStream(ze); 
     ret = new byte[ze.Size]; 
     s.Read(ret, 0, ret.Length); 
    } 

    return ret; 
} 
+0

Thanks that works – schmoopy 2010-06-04 02:28:09