1

そのため、PCでzipファイルを解凍しようとするとエラーが発生します。

7zip エラーは - ファイル ポインタをファイルの先頭より前に移動しようとしています。

UPD - ftp送信後、アーカイブ送信前にエラーが発生しても大丈夫!

ftp で作業するために、私は ClientFTP lib を使用しています。

まず、自分のデバイスで gps 座標を含むテキスト ファイルを作成し、それを圧縮して ftp にアップロードしようとしています。

次に、PCにダウンロードして解凍します。

アーカイブを作成するために多くの方法を試しました

テキストファイルを作成するための以下のコード

 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");

英語が下手で申し訳ありませんが、このエラーが発生する理由を教えてください。

4

0 に答える 0