2012-10-03 2 views
0

NIO2를 사용하여 소스 디렉토리의 내용에서 zip 파일을 생성 중입니다. 나는 ZipFileSystem을 사용하고 있는데, 먼저 인스턴스를 얻은 다음 경로를 생성해야합니다. 생성 된 경로는 Files.createDirectory(pathInZip) 또는 Files.copy(sourcePath, destPathInZip)을 사용하여 zip 파일에 항목을 만드는 데 사용할 수 있습니다. 이것은 잘 작동하지만 내가 피하려는 uglyness의 순간이 :Java nio2 : toString에 의존하지 않고 파일 시스템 간 경로 객체를 매핑하십시오.

// within the SimpleFileVisitor that walks through sourceDirFile 
@Override 
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { 
    Path pathInZip = zipFileSystem.getPath(sourceDirPath.relativize(file).toString()); // <-- ?! 
    Files.copy(file, pathInZip); 
    return FileVisitResult.CONTINUE; 
} 

는 서로의 경로로 하나 FileSystemProvider에서 경로를 복사 할 수있는 방법이 aPath.toString()에 의존하지 않고 있나요?. 추한 것 같습니다. 나는 한 경로를 반복하면서 점진적으로 다른 경로를 만들 수는 있지만 ...이 게시물을 쓸 시간이 걸린 FileSystem.getPath (Path anotherPath)를 갖는 것은 매우 쉬운 것처럼 보일 것입니다.

+1

분명히 그들 사이의 경로를 매핑하는 일반적인 작업으로 생각하지 않았다 파일 시스템. – irreputable

+0

은 감독처럼 보입니다. 그들이 샘플 처리 방법을보기 위해 샘플 zip 구현으로 넘어갈 것입니다. – tucuxi

답변

1

나는 이것을위한 몇 가지 유틸리티 방법을 작성했다. 어쩌면 당신은 그것들이 유용하다는 것을 알게 될 것입니다 (도서관은 오픈 소스입니다).

자습서 : http://softsmithy.sourceforge.net/lib/current/docs/tutorial/nio-file/index.html

자바 독 : http://softsmithy.sourceforge.net/lib/current/docs/api/softsmithy-lib-core/index.html

메이븐 :

<dependency> 
    <groupId>org.softsmithy.lib</groupId> 
    <artifactId>softsmithy-lib-core</artifactId> 
    <version>0.2</version> 
</dependency> 

상세 정보 : http://puces-blog.blogspot.ch/2012/07/news-from-software-smithy-version-02.html

+0

마침내 답을 다시 찾게되었습니다. 멋진 도서관!. – tucuxi

1

저는 아래에있는 dirutils lib를 사용하고 있습니다. 나는 그것을하려고 생각하고 있습니다. 그것은() toPath.resolve를 사용

https://github.com/bbejeck/Java-7/blob/master/src/main/java/bbejeck/nio/files/visitor/CopyDirVisitor.java

편집 : 당신이 내 코드를 다시 검토하고 내가 lib 디렉토리의 바로 그 부분을 패치 한 발견을 말하는 것으로 지금 롤. 하나는 일을 너무 쉽게 잊어 버린다 ..

@Override 
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { 

    Path relativizedPath =fromPath.relativize(dir); 

    Path targetPath = toPath.resolve(relativizedPath.toString()); 
    if(!Files.exists(targetPath)){ 
     Files.createDirectory(targetPath); 
    } 
    return FileVisitResult.CONTINUE; 
} 
+2

좋은 코드 -하지만 파일 시스템을 넘어서서 작동하지 않습니다. 경로가 파일 시스템에 연결되어있는 것으로 보입니다. – tucuxi

관련 문제