简体   繁体   English

如何移动使用Java.NIO文件系统创建的.zip文件夹

[英]How to move .zip folders created using Java.NIO FileSystem

I've created a .zip folder (compressed folder) using FileSystem, which is present in Java.nio package present in JDK 1.7 onwards. 我使用FileSystem创建了一个.zip文件夹(压缩文件夹),该文件存在于JDK 1.7及更高版本的Java.nio包中。

        URI zipUri = new URI("jar:" + fileUri.getScheme(), fileUri.getPath(), null);
        FileSystem zipfs = FileSystems.newFileSystem(zipUri, env);

Now, I want to move the zipped folders from one directory to another, but I couldn't find any way to locate the zipped folders because it is a FileSystem and there is no method present to move it. 现在,我想将压缩文件夹从一个目录移动到另一个目录,但是我找不到任何找到该压缩文件夹的方法,因为它是FileSystem,并且没有移动它的方法。

Files.move() works only with either file or directory, but not with zipped folders created from FileSystem. Files.move()仅适用于文件或目录,不适用于从FileSystem创建的压缩文件夹。

Can anyone point me to the right direction pls? 有人能指出我正确的方向吗?

How to move file to another directory in Java 如何将文件移动到Java中的另一个目录

Java.io.File does not contains any ready make move file method, but you can workaround with the following two alternatives : Java.io.File不包含任何现成的移动文件方法,但是您可以使用以下两种替代方法来解决:

File.renameTo(). File.renameTo()。 Copy to new file and delete the original file. 复制到新文件并删除原始文件。 In the below two examples, you move a file “C:\\carpeta1\\archivo.zip” from one directory to another directory with the same file name “C:\\carpeta2\\archivo2.zip“. 在下面的两个示例中,将文件“ C:\\ carpeta1 \\ archivo.zip”从一个目录移动到另一个目录,且文件名为“ C:\\ carpeta2 \\ archivo2.zip”。

package com.softMolina.zip;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class MoveZipExample
{
    public static void main(String[] args)
    {

    InputStream inStream = null;
    OutputStream outStream = null;

        try{

            File carpetaA =new File("C:\\carpeta1\\archivo.txt");
            File carpetaB =new File("C:\\carpeta2\\archivo.txt");

            inStream = new FileInputStream(carpetaA);
            outStream = new FileOutputStream(carpetaB);

            byte[] buffer = new byte[1024];

            int length;
            //copy the file content in bytes
            while ((length = inStream.read(buffer)) > 0){

                outStream.write(buffer, 0, length);

            }

            inStream.close();
            outStream.close();

            //delete the original file
            afile.delete();

            System.out.println(".ZIP is copied successful!");

        }catch(IOException e){
            e.printStackTrace();
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM