日期:2014-05-20  浏览次数:20657 次

java如何利用SmbFile读取远程服务器的zip包中的文件。已经做到了远程读整个文件,如何只读zip中的指定文件
jcifs.smb.SmbFile smbFile = new SmbFile("smb://root:admin@192.168.1.200/data.zip"); 
已经可以获取到data.zip,但是zip比较大下载到本地再用zipFile打开,速度比较慢。
如何能够在通过smb协议下载时,就只下载zip中指定的一个文件,假如已经知道zip包中的文件名。

------解决方案--------------------
Java code
SmbFile smbFile = new SmbFile(zipFile);
            System.out.println("smbFile:--"+zipFile);
            SmbFileInputStream in = new SmbFileInputStream(smbFile);
            ZipInputStream zip = new ZipInputStream(in);
            ZipEntry entry = null;
            boolean exist = false;
            while((entry = zip.getNextEntry()) != null)
            {
                System.out.println(entry.getName());
                if(entry.getName().equals(filename)){
                     response.setContentType("application/octet-stream");
                     response.setHeader("Content-disposition", "attachment; filename=" + filename);
                     OutputStream out = response.getOutputStream();
                     byte[] b = new byte[1024];
                      int n;
                      while((n = zip.read(b)) != -1)
                      {
                         out.write(b, 0, n);
                      }
                      out.flush();
                      out.close();
                      exist = true;
                      break;
                }

              }
              zip.close();