下面是压缩方法,求一个对压缩后的byte[]的解压方法public byte[] ZipPackFiles(string filenames)
{
ZipOutputStream zos = null;
MemoryStream ms = null;
try
{
ms = new MemoryStream();
zos = new ZipOutputStream(ms);
zos.SetLevel(9);
FileStream fs = File.OpenRead(filenames);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
ZipEntry entry = new ZipEntry(filenames);
entry.DateTime = DateTime.Now;
entry.Size = fs.Length;
fs.Close();
zos.PutNextEntry(entry);
zos.Write(buffer, 0, buffer.Length);
zos.Finish();
zos.Close();
return ms.ToArray();
}
catch (Exception)
{
return null;
}
}
{
ZipOutputStream zos = null;
MemoryStream ms = null;
try
{
ms = new MemoryStream();
zos = new ZipOutputStream(ms);
zos.SetLevel(9);
FileStream fs = File.OpenRead(filenames);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
ZipEntry entry = new ZipEntry(filenames);
entry.DateTime = DateTime.Now;
entry.Size = fs.Length;
fs.Close();
zos.PutNextEntry(entry);
zos.Write(buffer, 0, buffer.Length);
zos.Finish();
zos.Close();
return ms.ToArray();
}
catch (Exception)
{
return null;
}
}
{
err = "";
if (zipFilePath == string.Empty)
{
err = "压缩文件不能为空!";
return false;
}
if (!File.Exists(zipFilePath))
{
err = "压缩文件不存在!";
return false;
}
if (unZipDir == string.Empty)
unZipDir = zipFilePath.Replace(Path.GetFileName(zipFilePath), Path.GetFileNameWithoutExtension(zipFilePath));
if (!unZipDir.EndsWith("\\"))
unZipDir += "\\";
if (!Directory.Exists(unZipDir))
Directory.CreateDirectory(unZipDir); try
{
using (ZipInputStream s = new ZipInputStream(File.OpenRead(zipFilePath)))
{ ZipEntry theEntry;
while ((theEntry = s.GetNextEntry()) != null)
{
string directoryName = Path.GetDirectoryName(theEntry.Name);
string fileName = Path.GetFileName(theEntry.Name);
if (directoryName.Length > 0)
{
Directory.CreateDirectory(unZipDir + directoryName);
}
if (!directoryName.EndsWith("\\"))
directoryName += "\\";
if (fileName != String.Empty)
{
using (FileStream streamWriter = File.Create(unZipDir + theEntry.Name))
{ int size = 2048;
byte[] data = new byte[2048];
while (true)
{
size = s.Read(data, 0, data.Length);
if (size > 0)
{
streamWriter.Write(data, 0, size);
}
else
{
break;
}
}
}
}
}
}
}
catch (Exception ex)
{
err = ex.Message;
return false;
}
return true;
}
压缩解压缩
我要的是对压缩后的byte[]的解压方法
上面我给出了把文件压缩成byte[]的方法,现在要的是对byte[]的解压缩
if(compressed == null)
return null;
ByteArrayOutputStream out = null;
ByteArrayInputStream in = null;
ZipInputStream zin = null;
String decompressed;
try {
out = new ByteArrayOutputStream();
in = new ByteArrayInputStream(compressed);
zin = new ZipInputStream(new ByteArrayInputStream(compressed)); ZipEntry entry = zin.getNextEntry();
// 50K
byte[] buffer = new byte[1024];
int offset = 0;
System.out.println("getnext=="+zin.getNextEntry());
System.out.println("offset="+zin.read(buffer));
while((offset = zin.read(buffer)) != -1) {
out.write(buffer, 0, offset);
}
decompressed = out.toString();
} catch (IOException e) {
decompressed = null;
} finally {
if(zin != null) {
try {zin.close();} catch(IOException e) {}
}
if(in != null) {
try {in.close();} catch(IOException e) {}
}
if(out != null) {
try {out.close();} catch(IOException e) {}
}
}
return decompressed;
}