2011-10-27 3 views
0

최근 질문을 살펴 보았지만 문제에 대한 해결책을 찾을 수 없습니다. 나는 모든 내용 (기본적으로 텍스트 파일)을 포함하는 여러 디렉토리를 포함하는 디렉토리를 압축해야합니다. 우편 번호를 열면 동일한 디렉토리 목록을 다시 가져 오려고합니다.Windows 디렉터리를 zip 파일로 유지

내 문제는 콘텐츠를 압축 할 수 있지만 내 zip 파일 중 하나가 일반 파일 (디렉터리 없음)으로 나오거나 손상된 것입니다. 아무도이 짓을 한거야?

내가 게시하는 코드가 손상되었거나 겉으로보기에는 빈 우편 번호를 생성하고 있습니다. 여기에 내가 해결책을 발견 addDir

File[] files = dirObj.listFiles(); 
    byte[] tmpBuf = new byte[1024]; 

    for (int i = 0; i < files.length; i++) { 
     if (files[i].isDirectory()) { 
     addDir(files[i], out); 
     continue; 
     } 
     FileInputStream in = new FileInputStream(files[i].getAbsolutePath()); 
     System.out.println(" Adding: " + files[i].getAbsolutePath()); 
     out.putNextEntry(new ZipEntry(files[i].getAbsolutePath())); 
     int len; 
     while ((len = in.read(tmpBuf)) > 0) { 
     out.write(tmpBuf, 0, len); 
     } 
     out.closeEntry(); 
     in.close(); 
    } 

홈페이지

zipDir(filename, properties); 
+1

중복 가능성 [java.util.zip - 재생성 디렉토리 구조 (http://stackoverflow.com/questions/1399126/java-util-zip-recreating-directory-structure) –

+0

I는 솔루션을 가지고 , 그러나 나는 아직 그것을 게시 할 수 없다. 나는 내일 게시 할 것이다. – Jeff

답변

1

File dirObj = new File(fileDirectory); 
    String outFilename = zipDirectory+File.separatorChar+filename+".zip"; 
    log.info("Zip Directory: " + outFilename); 
    ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outFilename)); 
    System.out.println("Creating : " + outFilename); 
    addDir(dirObj, out); 
    out.close(); 

zipDir

공개 (요약) 내 방법이 있습니다. 이 코드는 내가 필요한 것을 정확하게 수행한다. 디렉토리를 압축하고 디렉토리 구조를 유지 관리합니다.

import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import java.util.zip.ZipEntry; 
import java.util.zip.ZipOutputStream; 

public class FolderZiper { 
    public static void main(String[] a) throws Exception { 
    zipFolder("c:\\a", "c:\\a.zip"); 
    } 

    static public void zipFolder(String srcFolder, String destZipFile) throws Exception { 
    ZipOutputStream zip = null; 
    FileOutputStream fileWriter = null; 

    fileWriter = new FileOutputStream(destZipFile); 
    zip = new ZipOutputStream(fileWriter); 

    addFolderToZip("", srcFolder, zip); 
    zip.flush(); 
    zip.close(); 
    } 

    static private void addFileToZip(String path, String srcFile, ZipOutputStream zip) 
     throws Exception { 

    File folder = new File(srcFile); 
    if (folder.isDirectory()) { 
     addFolderToZip(path, srcFile, zip); 
    } else { 
     byte[] buf = new byte[1024]; 
     int len; 
     FileInputStream in = new FileInputStream(srcFile); 
     zip.putNextEntry(new ZipEntry(path + "/" + folder.getName())); 
     while ((len = in.read(buf)) > 0) { 
     zip.write(buf, 0, len); 
     } 
    } 
    } 

    static private void addFolderToZip(String path, String srcFolder, ZipOutputStream zip) 
     throws Exception { 
    File folder = new File(srcFolder); 

    for (String fileName : folder.list()) { 
     if (path.equals("")) { 
     addFileToZip(folder.getName(), srcFolder + "/" + fileName, zip); 
     } else { 
     addFileToZip(path + "/" + folder.getName(), srcFolder + "/" + fileName, zip); 
     } 
    } 
    } 
} 
관련 문제