2010-11-07 116 views
2

我正在从数据库获取某些文件的完整路径,然后在该路径中压缩这些文件。因此,问题在于路径包含通配符,即使是目录和文件也是如此。 如:带路径的FileInputStream包含通配符

/myfolder/product/test/*.xml 
or 
/myfolder/*/*.xml 

所以,我怎样才能得到这些XML文件驻留在该路径下的文件输入流以后压缩它们?

如果我没有通配符,只有路径代表文件所在的目录,我按照如下方式添加每个文件。但问题是我的路径包含带有通配符的文件名。

 //List of the files in the directry 
     File f = new File("/folder"); 
     String files[] = f.list(); 
     BufferedInputStream in = null; 

     for(int i =0;i<files.length;i++){ 
     in = new BufferedInputStream(new FileInputStream("/folder/"+files[i]), 1000); 
     out.putNextEntry(new ZipEntry("somepath/"+files[i])); 
     int count; 

     while ((count = in.read(data, 0, 1000)) != -1) { 
      out.write(data, 0, count); 
     } 
     } 

谢谢。

回答

3

通配符通常由shell解释。您可以使用java.nio.file.FileSystem.getPathMatcher()在Java中执行类似的操作。有关更多信息和用法示例,请参阅this tutorial

+1

小纸条:java.nio.file包是在Java 7中添加的。 – lhballoti 2010-11-07 21:13:28

+0

有没有办法用java 6做到这一点 – Harshana 2010-11-08 00:54:26