解包zip错误android

时间:2012-11-19 09:24:28

标签: java android zip

当我试图在个人电脑上解压缩我的zip文件时,我有错误。

7zip错误是 - 尝试在文件开头之前移动文件指针。

UPD - 发送到ftp后发生错误,然后发送存档就可以了!

使用ftp工作我使用ClientFTP lib。

首先,在我的设备上,我使用gps坐标创建一个文本文件,然后我尝试压缩它并上传到ftp

然后下载到电脑并解压缩。

我尝试了很多方法来创建存档

下面的代码创建一个文本文件

 File file;
  try{
    file = new File(path, "locations.lft");
    if (file.exists())
      file.delete();
      file.createNewFile();
    }catch (Exception e){
    try{ftp.quit();}catch(Exception E){}
      return;
    }
   BufferedWriter BW = null;
   try{
BW = new BufferedWriter(new FileWriter(file));
}catch(Exception e){
try{ftp.quit();}catch(Exception E){}
    return;
}
Cursor row = db.Query("locations",null,"date_time BETWEEN " + ToBeggingOfDay(date).getTime() + " AND " + 
                    ToEndOfDay(date).getTime());
try{
   while (row.moveToNext()){
   String line = "" + row.getString(row.getColumnIndex("latitude")) + "~" +  row.getString(row.getColumnIndex("longtitude")) + "~"  + DateFormat.format("yyyy-MM-dd-kk-mm-ss", new Date( row.getLong(row.getColumnIndex("date_time")) )) + "~" + "0~~~~"+row.getString(row.getColumnIndex("speed"));
       BW.write(line);
   BW.newLine();
 }
}catch (Exception e){
SendMsg("ERROR", "Can't write in file (" + e.getLocalizedMessage() + ")");
try{ftp.quit();}catch(Exception E){}
  try{BW.close();}catch(Exception E){}
return;
}finally{
row.close();
try{BW.close();}catch(Exception E){}
 }

包zip文件的代码

    public class ZipUtil {
        /**
         * A constants for buffer size used to read/write data
         */
        private static final int BUFFER_SIZE = 4096;

        /**
         * Compresses a collection of files to a destination zip file
         * @param listFiles A collection of files and directories
         * @param destZipFile The path of the destination zip file
         * @throws FileNotFoundException
         * @throws IOException
         */
       public void compressFiles(List<File> listFiles, String destZipFile) throws FileNotFoundException, IOException {

           ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(destZipFile));

           for (File file : listFiles) {
               if (file.isDirectory()) {
                   addFolderToZip(file, file.getName(), zos);
               } else {
                   addFileToZip(file, zos);
               }
           }

           zos.flush();
           zos.close();
       }

       /**
        * Adds a directory to the current zip output stream
        * @param folder the directory to be  added
        * @param parentFolder the path of parent directory
        * @param zos the current zip output stream
        * @throws FileNotFoundException
        * @throws IOException
        */
        private void addFolderToZip(File folder, String parentFolder,
                ZipOutputStream zos) throws FileNotFoundException, IOException {
            for (File file : folder.listFiles()) {
                if (file.isDirectory()) {
                    addFolderToZip(file, parentFolder + "/" + file.getName(), zos);
                    continue;
                }

                zos.putNextEntry(new ZipEntry(parentFolder + "/" + file.getName()));

                BufferedInputStream bis = new BufferedInputStream(
                        new FileInputStream(file));

                long bytesRead = 0;
                byte[] bytesIn = new byte[BUFFER_SIZE];
                int read = 0;

                while ((read = bis.read(bytesIn)) != -1) {
                    zos.write(bytesIn, 0, read);
                    bytesRead += read;
                }

                zos.closeEntry();

            }
        }

        /**
         * Adds a file to the current zip output stream
         * @param file the file to be added
         * @param zos the current zip output stream
         * @throws FileNotFoundException
         * @throws IOException
         */
        private void addFileToZip(File file, ZipOutputStream zos)
                throws FileNotFoundException, IOException {
            zos.putNextEntry(new ZipEntry(file.getName()));

            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
                    file));

            long bytesRead = 0;
            byte[] bytesIn = new byte[BUFFER_SIZE];
            int read = 0;

            while ((read = bis.read(bytesIn)) != -1) {
                zos.write(bytesIn, 0, read);
                bytesRead += read;
            }

            zos.closeEntry();
        }
    }

发送到FTP的代码末尾

                BufferedInputStream bis = null;
            try{
                bis = new BufferedInputStream(new FileInputStream(file));
                if (!ftp.storeFile(FTPFileName, bis)){
                    SendMsg("ERROR", "Can't write in file");
                    return;
                }
            }catch (Exception e) {
                SendMsg("ERROR", "Can't write in file");
                return;
            }finally{
                try{bis.close();}catch (Exception E) {}
                file.delete();
            }

实施例

                        try {

                           ZipUtil zipper = new ZipUtil();
                            File directoryToZip = new File(path+"locations.lft");
                            String zipFilePath = path + "1.zip";
                            List<File> listFiles = new ArrayList<File>(1);
                            listFiles.add(directoryToZip);

                            zipper.compressFiles(listFiles, zipFilePath);
                        }catch(Exception e){
                             e.printStackTrace();
                        }
                        file = new File(path, "1.zip");

抱歉我的英文不好,希望你有一些想法,为什么我会收到这个错误

0 个答案:

没有答案