使用java api从clojure读取zip文件 [英] Reading a zip file using java api from clojure
本文介绍了使用java api从clojure读取zip文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
public class ZipFileRdrExp {
public static void main(String [] args){
try {
FileInputStream fis = new FileInputStream(C:\\MyZip.zip);
ZipInputStream zis = new ZipInputStream(fis);
ZipEntry ze;
while((ze = zis.getNextEntry())!= null){
System.out.println(ze.getName());
zis.closeEntry();
}
zis.close();
} catch(FileNotFoundException e){
e.printStackTrace();
} catch(IOException e){
e.printStackTrace();
}
}
}
重复调用getNextEntry:
(ns app.core
(:import
(java.io FileInputStream FileNotFoundException IOException File)
(java.util.zip ZipInputStream ZipEntry)))
(defn-read-zip [zip-file]
(let [fis(FileInputStream。zip-file)
zis(ZipInputStream.fis)]
(loop [ze(.getNextEntry zis)]
(when ze
(println(.getName ze))
(.closeEntry zis)
(recur(.getNextEntry zis))))
(.close zis)))
解决方案我会用类似下面的方法:
(defn entries [zipfile]
(lazy-seq
(if-let [entry(.getNextEntry zipfile) ]
(cons entry(entries zipfile)))))
(defn walkzip [fileName]
(with-open [z(ZipInputStream。 (FileInputStream.fileName))]
(doseq [e(entries z)]
(println(.getName e))
(.closeEntry z)))
编辑:上述代码最终被测试并更正。
EDIT:,下面的工作方式与预期的一样,即使它使用不同的Java API也更加简洁。
(defn walkzip [fileName]);
(enumeration-seq(.entries zipfile)
(with-open [z(java.util.zip.ZipFile.fileName)]
(doseq [e(entries z)]
(println(.getName e)))))
I'm trying to rewrite following snippet in clojure, but it all comes out ugly, maybe someone will suggest a more elegant solution?
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; public class ZipFileRdrExp { public static void main(String[] args) { try { FileInputStream fis = new FileInputStream("C:\\MyZip.zip"); ZipInputStream zis = new ZipInputStream(fis); ZipEntry ze; while((ze=zis.getNextEntry())!=null){ System.out.println(ze.getName()); zis.closeEntry(); } zis.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
Here is my ugly try with duplicate call to getNextEntry:
(ns app.core (:import (java.io FileInputStream FileNotFoundException IOException File) (java.util.zip ZipInputStream ZipEntry))) (defn- read-zip [zip-file] (let [fis (FileInputStream. zip-file) zis (ZipInputStream. fis)] (loop [ze (.getNextEntry zis)] (when ze (println (.getName ze)) (.closeEntry zis) (recur (.getNextEntry zis)))) (.close zis)))
解决方案I would go with something like the following:
(defn entries [zipfile] (lazy-seq (if-let [entry (.getNextEntry zipfile)] (cons entry (entries zipfile))))) (defn walkzip [fileName] (with-open [z (ZipInputStream. (FileInputStream. fileName))] (doseq [e (entries z)] (println (.getName e)) (.closeEntry z))))
EDIT: the above code was eventually tested and corrected.
EDIT: the following works as expected and it's much more concise, even though it uses a different Java API
(defn entries [zipfile] (enumeration-seq (.entries zipfile))) (defn walkzip [fileName] (with-open [z (java.util.zip.ZipFile. fileName)] (doseq [e (entries z)] (println (.getName e)))))
这篇关于使用java api从clojure读取zip文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文