Java的 - 如何将文件移动到一个zip文件? [英] Java - How to move a file into a zip file?

查看:178
本文介绍了Java的 - 如何将文件移动到一个zip文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

就是这样。我有一个文本文件,我需要把它移动到一个给定的目录(现有)的Zip文件。

 文件文件=新文件(C:\\文件夹\\测试.txt); 
文件dir = new File(directoryToGo +existingzipfile.zip);
boolean success = file.renameTo(new File(dir,file.getName()));

但是不行。有没有办法将文件移动到现有的Zip文件?
谢谢。

解决方案

嗯,你可以使用类似的东西:


$ b $ (文件zipFile,File [] files)抛出IOException {
//获取临时文件
文件tempFile = File.createTempFile(File.createTempFile zipFile.getName(),null);
//删除它,否则你不能重命名现有的zip到它。
tempFile.delete();
布尔renameOk = zipFile.renameTo(tempFile); (!renameOk){
throw new RuntimeException(无法重命名文件+ zipFile.getAbsolutePath()+to+ tempFile.getAbsolutePath());
}
byte [] buf = new byte [1024];
ZipInputStream zin = new ZipInputStream(new FileInputStream(tempFile));
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFile));
ZipEntry entry = zin.getNextEntry();
while(entry
!= null){
String name = entry.getName();
boolean notInFiles = true;
for(File f:files){
if(f.getName()。equals(name)){
notInFiles = false;
break;


if(notInFiles){//将ZIP条目添加到输出流。 out.putNextEntry(new ZipEntry(name)); //从ZIP文件传输字节到输出文件int len; while((len = zin.read(buf))> 0){out.write(buf,0,len); }} entry = zin.getNextEntry(); } //关闭流zin.close(); //压缩文件(int i = 0; i< files.length; i ++){InputStream in = new FileInputStream(files [i]); //将ZIP条目添加到输出流。 out.putNextEntry(new ZipEntry(files [i] .getName())); //将文件中的字节传输到ZIP文件int len; while((len = in.read(buf))> 0){out.write(buf,0,len); } //完成条目out.closeEntry();附寄(); } //完成ZIP文件out.close();
tempFile.delete();




参考: p>


That's it. I have a text file, and I need to move it to a (existing) Zip File in a given directory.

    File file = new File("C:\\afolder\\test.txt");
    File dir = new File(directoryToGo+"existingzipfile.zip");
    boolean success = file.renameTo(new File(dir, file.getName()));

But it does not work. Is there a way to move a file into a existing Zip File? Thank you.

解决方案

Hmm you could use something like:

public static void addFilesToExistingZip(File zipFile, File[] files) throws IOException {
    // get a temp file 
    File tempFile = File.createTempFile(zipFile.getName(), null);
    // delete it, otherwise you cannot rename your existing zip to it. 
    tempFile.delete();
    boolean renameOk = zipFile.renameTo(tempFile);
    if (!renameOk) {
        throw new RuntimeException("could not rename the file " + zipFile.getAbsolutePath() + " to " + tempFile.getAbsolutePath());
    }
    byte[] buf = new byte[1024];
    ZipInputStream zin = new ZipInputStream(new FileInputStream(tempFile));
    ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFile));
    ZipEntry entry = zin.getNextEntry();
    while (entry
            != null) {
        String name = entry.getName();
        boolean notInFiles = true;
        for (File f : files) {
            if (f.getName().equals(name)) {
                notInFiles = false;
                break;
            }
        }
        if (notInFiles) { // Add ZIP entry to output stream. out.putNextEntry(new ZipEntry(name)); // Transfer bytes from the ZIP file to the output file int len; while ((len = zin.read(buf)) > 0) { out.write(buf, 0, len); } } entry = zin.getNextEntry(); } // Close the streams    zin.close(); // Compress the files for (int i = 0; i < files.length; i++) { InputStream in = new FileInputStream(files[i]); // Add ZIP entry to output stream. out.putNextEntry(new ZipEntry(files[i].getName())); // Transfer bytes from the file to the ZIP file int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } // Complete the entry out.closeEntry(); in.close(); } // Complete the ZIP file out.close(); 
            tempFile.delete();
        }
    }

Reference:

这篇关于Java的 - 如何将文件移动到一个zip文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆