所以当我试图在电脑上解压我的 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");
对不起我的英语不好,希望你知道为什么我会收到这个错误