powered by simpleCommunicator - 2.0.61     © 2026 Programmizd 02
Целевая тема:
Создать новую тему:
Автор:
Закрыть
Цитировать
Форумы / Java [игнор отключен] [закрыт для гостей] / zipit
4 сообщений из 4, страница 1 из 1
zipit
    #32895537
Naug
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
Я тут пытаюсь засиповать одну папочку (которая имеет вложенные папки)

Код: plaintext
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
    static   public   void  zipDir(String dirName, String zipName) {
         try  {
            //create a ZipOutputStream to zip the data to
            ZipOutputStream zos =  new  ZipOutputStream( new  FileOutputStream(zipName));
            //assuming that there is a directory named inFolder (If there
            //isn't create one) in the same directory as the one the code     runs from,
            //call the zipDir method
            zipDir(dirName, zos);
            //close the stream
            zos.close();
        }  catch  (Exception e) {
            e.printStackTrace();
        }
    }


     public   static   void  zipDir(String dir2zip, ZipOutputStream zos) {
         try  {
            //create a new File object based on the directory we

            File zipDir =  new  File(dir2zip);
            //get a listing of the directory content
            String[] dirList = zipDir.list();
             byte [] readBuffer =  new   byte [ 2156 ];
             int  bytesIn =  0 ;
            //loop through dirList, and zip the files
             for  ( int  i =  0 ; i < dirList.length; i++) {
                File f =  new  File(zipDir, dirList[i]);
                System.out.println(f.getAbsolutePath());
                 if  (f.isDirectory()) {
                    //if the File object is a directory, call this
                    //function again to add its content recursively
                    String filePath = f.getPath();
                    zipDir(filePath, zos);
                    //loop again
                     continue ;
                }
                //if we reached here, the File object f was not a directory
                //create a FileInputStream on top of f
                FileInputStream fis =  new  FileInputStream(f);
                //create a new zip  entry
                ZipEntry anEntry =  new  ZipEntry(f.getPath());
                //place the zip entry in the ZipOutputStream object
                zos.putNextEntry(anEntry);
                //now write the content of the file to the ZipOutputStream
                 while  ((bytesIn = fis.read(readBuffer)) != - 1 ) {
                    zos.write(readBuffer,  0 , bytesIn);
                }
                //close the Stream
                fis.close();
            }
        }  catch  (Exception e) {
            //handle exception
        }
    }

Значит, проблема в том что в зип попадает вся папка целиком, а мне нужно только её содержимое (без самой первой папки). Звиняюсь если ворос тупой, но у меня небольшой ахтунг
...
Рейтинг: 0 / 0
zipit
    #32895574
Фотография mayton
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
Здесь f.getPath() обычно возвращает что-то вроде "...{folder}/{file.ext}"

Код: plaintext
1.
2.
3.
4.
5.
6.
...
//create a FileInputStream on top of f
FileInputStream fis = new FileInputStream(f);
//create a new zip  entry
ZipEntry anEntry = new ZipEntry(f.getPath());
...

ИМХО тебе надо удалить "...{folder}" и оставить только "{file.ext}"
...
Рейтинг: 0 / 0
zipit
    #32896326
Naug
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
так все файлы свалятся в одну кучу. Более аккуратно -
Код: plaintext
 ZipEntry anEntry =  new  ZipEntry(f.getPath().substring(f.getPath().indexOf(File.separatorChar)+ 1  ));
...
Рейтинг: 0 / 0
zipit
    #32897423
Фотография mayton
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
Не возражаю
...
Рейтинг: 0 / 0
4 сообщений из 4, страница 1 из 1
Форумы / Java [игнор отключен] [закрыт для гостей] / zipit
Найденые пользователи ...
Разблокировать пользователей ...
Читали форум (0):
Пользователи онлайн (0):
x
x
Закрыть


Просмотр
0 / 0
Close
Debug Console [Select Text]