Combine files into one file and then separate them

xuanbao · · 524 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>For example, I have 5 files in my folder. How can I combine them into one file with first file name? any libraries available for this? </p> <p>End of my business logic, I want them into 5 separate files again.</p> <hr/>**评论:**<br/><br/>abramsimon: <pre><p>You could tar them at the beginning to produce a single archive file, then untar them at the end. Here is a good breakdown of both: </p> <p><a href="https://medium.com/@skdomino/taring-untaring-files-in-go-6b07cf56bc07?source=linkShare-a7d8ec2f59bb-1522794712" rel="nofollow">https://medium.com/@skdomino/taring-untaring-files-in-go-6b07cf56bc07?source=linkShare-a7d8ec2f59bb-1522794712</a></p></pre>INVOKECloud: <pre><p>Seems an interesting option. One thing I am not sure after reading the link is, result of the tar is single file (or) something like compressed but 5 different files?</p></pre>abramsimon: <pre><p>tar.NewWriter(io.Writer) writes it’s output to a single file, assuming the writer you pass it is a file (for example, from a call to os.Create())</p> <p>The example article I provided can be a little confusing on that because they use an io.MultiWriter to output to multiple targets</p> <blockquote> <p>the purpose for accepting multiple writers is to allow for multiple outputs (for example a file, or md5 hash)</p> </blockquote></pre>abramsimon: <pre><p>Comparing to the other solutions provided, using this tar approach has two benefits: 1. The use of io.Copy() means there is limited memory used, so large files are not a problem. 2. The tar headers give a clear way to extract individual files back out into their original form.</p></pre>balacode: <pre><p>You don&#39;t really need a library for this, just a few lines of Go code. If you want, send me a message with more details of what you want to do and I&#39;ll write up some simple Go function to join and split the files, and post it on github </p></pre>balacode: <pre><p>The simplest method is use ioutil.ReadFile() to read each file, then append its bytes to a byte slice or bytes.Buffer, finally use ioutil.WriteFile() to write the joined byte slice or buffer to the file you want. This assumes the files you&#39;re joining are not very big and can be accommodated in RAM. If the files are large, then we can read small chunks from each file and append to a temp fle. Finally rename the temp file to the desired name.</p></pre>balacode: <pre><p>Some questions: When you&#39;re separating the files at the end, you&#39;re separating them based on what criteria? What happens after you join the files, before splitting them back? What are you manipulating? Are the files text files?</p></pre>INVOKECloud: <pre><p>I sent you private message with details. Thanks for your help!</p></pre>raff99: <pre><p>Go standard library also provides a &#34;multi reader&#34;: <a href="https://golang.org/pkg/io/#MultiReader" rel="nofollow">https://golang.org/pkg/io/#MultiReader</a></p> <p>Create a multi-reader with the files in the order you want, read from the reader, write to a writer.</p> <p>Not sure of what you mean by &#34;end of business logic I want them into 5 separate files&#34;. If you didn&#39;t modify they original, they are already &#34;separated&#34;. If you want to split the new output into 5 files, you need to figure out where you want to split the input.</p></pre>INVOKECloud: <pre><p>Thanks for pointers. I want to delete original files, which is why I said &#34;end of logic, I need them back&#34;. Yes, I am Ok to add a delimiter to keep track of file separation.</p></pre>Justinsaccount: <pre><p><a href="https://golang.org/pkg/archive/zip/" rel="nofollow">https://golang.org/pkg/archive/zip/</a></p></pre>INVOKECloud: <pre><p>My understanding of zip is compression, not combining them into 1 file. Am I missing something?</p></pre>mcouturier: <pre><p>I would read the 5 files each in a <code>ReadSeeker</code>. Then use <code>MultiReader</code> to read them as one file. Then seek the files to <code>0, io.SeekStart</code> to use them individually again.</p></pre>

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

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