filepath.walk and extra data

blov · · 993 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I&#39;m writing an application that scans for zip files using filepath.walk and when it finds a zip file I want it to extract it to a temp folder and search (using filepath.walk) its contents for a certain string.</p> <p>Should be simple but when the string is found I can not send anything extra to filepath.walk the function it has no knowledge of the original filepath, only the filepath of the zipped temp folder. </p> <p>Is there some way to do this properly? </p> <hr/>**评论:**<br/><br/>dchapes: <pre><blockquote> <p>extract it to a temp folder and search (using filepath.walk) its contents</p> </blockquote> <p>If you only need to report matching contents (e.g. <code>some/path/foo.zip</code> -&gt; <code>path/in/zip/bar.txt</code> matches) or something similar, then it would be <strong>much</strong> better to not muck with the file system and a temporary folder (with all the IO, time, and cleanup issues that would entail) but to use the <a href="https://golang.org/pkg/archive/zip" rel="nofollow"><code>archive/zip</code></a> package to <a href="https://golang.org/pkg/archive/zip/#example_Reader" rel="nofollow">scan through the zip file&#39;s contents</a> in memory without using the file system.</p></pre>metamatic: <pre><p>Particularly if someone puts a <a href="https://en.wikipedia.org/wiki/Zip_bomb" rel="nofollow">zip bomb</a> on your hard drive.</p></pre>Hexodam: <pre><p>Right now it&#39;s going to a temp folder, but the idea of a zip bomb is quite scary, unlikely in my case but you never know. </p> <p>Might use both depending on size because I don&#39;t want to use a lot of memory either. </p></pre>dchapes: <pre><blockquote> <p>I don&#39;t want to use a lot of memory</p> </blockquote> <p>I don&#39;t think you understand how <code>archive/zip</code> works. It doesn&#39;t unzip the full contents of anything into memory. Other than the tiny size of the header the zip file is streamed, if your reader is at all sane it will just look at the file line by line (or chunk by chunk) and will consume hardly any memory at all, no matter what the size of zip file.</p></pre>Hexodam: <pre><p>That seems to be the care with my tests, loving go more and more </p></pre>gogroob: <pre><p>Wrap your walk function and pass any parameters you need. </p> <pre><code>func walkFnWithExtraParams(myParam string) filepath.WalkFunc { return func(path string, info os.FileInfo, err error) error { // do something with myParam here } } </code></pre></pre>Hexodam: <pre><p>How would this work? </p> <p>Here is some test code I have</p> <p><a href="http://play.golang.org/p/jYXpgNwlXn" rel="nofollow">http://play.golang.org/p/jYXpgNwlXn</a></p></pre>gogroob: <pre><p>I&#39;m not quite sure what you&#39;re trying to do, but you&#39;d call it passing your walkfunc to <code>filepath.Walk</code> </p> <pre><code>filepath.Walk(&#34;/&#34;, walkFnWithExtraParams(&#34;/&#34;, &#34;extra&#34;)) </code></pre></pre>Hexodam: <pre><p>Ah, works perfectly now! :D</p> <p><a href="http://play.golang.org/p/160epwskqF" rel="nofollow">http://play.golang.org/p/160epwskqF</a></p></pre>tv64738: <pre><p>No need to go all Javascript with functions-returning-functions, just use a closure.</p> <pre><code>func walk(root string, extra string) error { fn := func(path string, info os.FileInfo, err error) error { fmt.Println(path+&#34; &#34;+extra) return nil } return filepath.WalkFunc(root, fn) } </code></pre></pre>hayzeus: <pre><p>Closures are your friend here</p></pre>Hexodam: <pre><p>I don&#39;t know that person, can you introduce me? :)</p></pre>hayzeus: <pre><p><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures" rel="nofollow">https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures</a></p> <p>For the general case where you need to provide a callback function, but the function signature doesn&#39;t take the parameters you need, you can take advantage of the fact that Go supports closures to either create the callback function inline, or make a higher-order function that returns the callback. </p> <p>You will use this technique a lot in Go. In <a href="http://play.golang.org/p/160epwskqF" rel="nofollow">http://play.golang.org/p/160epwskqF</a> walkFnWithExtraParams is a higher-order function -- a function that returns a function. walkFnWithExtraParams uses the fact that go supports closures to build the return function that has a reference to <code>extra</code></p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

993 次点击  
加入收藏 微博
0 回复
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传